datetime.date.today() -1 in python

To subtract one day from the current date in Python using the datetime module, you can use the date.today() function along with the timedelta function. Here's an example:

main.py
from datetime import date, timedelta

current_date = date.today()
previous_date = current_date - timedelta(days=1)

print(previous_date)
137 chars
7 lines

In the above code, date.today() returns the current date as a date object, and timedelta(days=1) creates a time delta of one day. Subtracting this time delta from the current date gives you the previous day's date.

Running the code will output the previous date in the format YYYY-MM-DD.

related categories

gistlibby LogSnag