how to get the day before in python

Viewed 29

I want the day before to the variable start, I have this code

now = datetime.now()
start = strttime(%Y-$m-%d"+"T00:00:00")

How can I solve it?

2 Answers

It can be accomplished using datetime.timedelta.

I found ananswer to a similar question, that would work well here. So below is the same answer, but modified to produce yesterday's date:

import datetime

yesterday = datetime.datetime.now() - datetime.timedelta(days=1)

Here you go:

from datetime import datetime, timedelta
    
N_DAYS_AGO = 1
    
today = datetime.now()    
n_days_ago = today - timedelta(days=N_DAYS_AGO)
print today, n_days_ago
Related