In this Python program, we are finding what was yesterday’s date with reference to today.
Python Program to get Yesterday’s date In Python
from datetime import date
from datetime import timedelta
# Get today's date
today = date.today()
print("Today is: ", today)
# Get Yesterday date
yesterday = today - timedelta(days = 1)
print("Yesterday was: ", yesterday)
Output:
Today is: 2023-02-18
Yesterday was: 2023-02-17
How does it work
- First, we need to import the date class from the datetime module
- The timedelta() function is present under datetime library which is generally used
- for calculating differences in dates and also can be used for date manipulations in Python.
- It is one of the easiest ways to perform date manipulations.