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

Operations on datetime objects

The datetime and timedelta classes support various mathematical operations to get dates in the future or the past. Using these operations returns another datetime object. . In this recipe, you would create datetime, date, time, and timedelta objects and perform mathematical operations on them.

How to do it…

Follow along with these steps to execute this recipe:

  1. Import the necessary modules from the Python standard library:
>>> from datetime import datetime, timedelta
  1. Fetch today's date. Assign it to date_today and print it:
>>> date_today = date.today()              
>>> print(f"Today's Date: {date_today}")

We get the following output. Your output may differ:

Today's Date: 2020-08-12
  1. Add 5 days to today's date using a timedelta object. Assign it to date_5days_later and print it:
>>> date_5days_later = date_today + timedelta(days=5)
>>> print(f"Date 5 days later: {date_5days_later...
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