[Python-Dev] How do you get yesterday from a time object

Larry Bates lbates at syscononline.com
Tue Apr 19 13:18:03 EDT 2005


Use datetime module if you are on 2.3 or 2.4, otherwise you
can do:

today=time.time()
yesterday=today-24*60*60  # 24 hours x 60 minutes x 60 seconds

Your problem is that time.localtime() converts to 9 value
tuple (you can't subtract seconds from a tuple).  You do
the math on today and convert using time.localtime after.

For future reference: Please post a copy of your traceback
instead of just saying "<-- generates and error".  It helps
people to respond more quickly and accurately.

-Larry Bates


Simon Brunning wrote:
> On 4/19/05, Ralph Hilton <flinkkettel at yahoo.com> wrote:
> 
>>i'm a beginning python programmer.
>>
>>I want to get the date for yesterday
>>
>>nowTime = time.localtime(time.time())
>>print nowTime.
>>oneDay = 60*60*24 # number seconds in a day
>>yday = nowTime - oneDay  # <-- generates an error
>>print yday.strftime("%Y-%m-%d")
> 
> 
> today = datetime.date.today()
> previous_working = today - datetime.timedelta(days=1)
> 



More information about the Python-list mailing list