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:
- Import the necessary modules from the Python standard library:
>>> from datetime import datetime, timedelta
- 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
- 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...