Finding yesterday's date with datetime

T

Thierry Lam

Is there an easy way to determine the yesterday's date(year-month-day)
from the python datetime library if I know today's date?

Thanks
Thierry
 
M

Michael J. Fromberger

"Thierry Lam said:
Is there an easy way to determine the yesterday's date(year-month-day)
from the python datetime library if I know today's date?

from datetime import datetime, timedelta

today = datetime.today()
yesterday = today - timedelta(1)

# See the .month, .day, and .year fields of yesterday

Cheers,
-M
 
A

andyrsmith

Try something like this...

HTH. A.


from datetime import *

d1 = datetime( year=2006, month=5, day=15)
d2 = datetime.now()

for d in [d1,d2]:
yest = d - timedelta(days =1 )

print "%s -> %s" % (d, yest)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top