Reputation: 189
I want a code snippet that give today's date and yesterday's date in YYYY-MM-DD format. I know how to extract the current date and here is the code for it:
import datetime
cur_date = datetime.datetime.today().strftime('%Y-%m-%d')
But I do not know how to extract the previous day's date. Is manipulating the current date the only method to do so?
Upvotes: 1
Views: 3595
Reputation: 376
You can do it using timedelta
:
from datetime import datetime, timedelta
yesterday = datetime.today() - timedelta(days=1)
Output will be :
datetime.datetime(2021, 1, 26, 20, 27, 25, 849797)
Upvotes: 3