Reader small image

You're reading from  Python Algorithmic Trading Cookbook

Product typeBook
Published inAug 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838989354
Edition1st Edition
Languages
Right arrow
Author (1)
Pushpak Dagade
Pushpak Dagade
author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade

Right arrow

Creating datetime objects

The datetime module provides a datetime class, which can be used to accurately capture information relating to timestamps, dates, times, and time zones. In this recipe, you will create datetime objects in multiple ways and introspect their attributes.

How to do it…

Follow these steps to execute this recipe:

  1. Import the necessary module from the Python standard library:
>>> from datetime import datetime
  1. Create a datetime object holding the current timestamp using the now() method and print it:
>>> dt1 = datetime.now()
>>> print(f'Approach #1: {dt1}')

We get the following output. Your output will differ:

Approach #1: 2020-08-12 20:55:39.680195
  1. Print the attributes of dt1 related to date and time:
>>> print(f'Year: {dt1.year}')
>>> print(f'Month: {dt1.month}')
>>> print(f'Day: {dt1.day}')
>>> print(f'Hours: {dt1.hour}')
>>> print(f&apos...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Python Algorithmic Trading Cookbook
Published in: Aug 2020Publisher: PacktISBN-13: 9781838989354

Author (1)

author image
Pushpak Dagade

Pushpak Dagade is working in the area of algorithmic trading with Python for more than 3 years. He is a co-founder and CEO of AlgoBulls, an algorithmic trading platform.
Read more about Pushpak Dagade