Velocity Reviews > Re: [Python-Dev] How do you get yesterday from a time object

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

Simon Brunning
Guest
Posts: n/a

 04-19-2005
On 4/19/05, Ralph Hilton <(E-Mail Removed)> 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)

--
Cheers,
Simon B,
http://www.velocityreviews.com/forums/(E-Mail Removed),
http://www.brunningonline.net/simon/blog/

Larry Bates
Guest
Posts: n/a

 04-19-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.

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 <(E-Mail Removed)> 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)
>