how to get datetime.date yesterday in python

To get the date of yesterday in Python, you can use the datetime module. Here's an example code:

main.py
import datetime

yesterday = datetime.date.today() - datetime.timedelta(days=1)
print(yesterday)
97 chars
5 lines

In this code, we first import the datetime module. Then, we use the datetime.date.today() function to get the current date and subtract datetime.timedelta(days=1) to get the date of yesterday.

The timedelta function allows you to specify a duration, in this case, 1 day. Subtracting this duration from the current date gives you the date of yesterday.

Finally, we print the yesterday variable to display the date of yesterday.

related categories

gistlibby LogSnag