Friday, October 13, 2006

Went looking around for how to get the date of yesterday in python. Here are two ways...

from time import gmtime, strftime

#Get yesterdays date and time (from http://www.thejackol.com/2005/07/01/yesterdays-date-in-python/)

now = time.time()
today = time.localtime(now)
yesterday = strftime('"%a-%m/%d/%Y"',time.localtime(now - 60*60*24))

or

import datetime
now = datetime.datetime.now()
one_day = datetime.timedelta(hours=24)
yesterday = now - one_day
print strftime('"%a-%m/%d/%Y"',yesterday.timetuple())

No comments: