A problem with Time

Gary Herron gherron at islandtraining.com
Thu Aug 16 12:05:48 EDT 2007


special_dragonfly wrote:
> Hello,
>
> I need to return the date yesterday in the form DDMMYYYY. I looked through 
> the modules: time, datetime and calendar but can't find anything that leaps 
> out at me.
>
> The problem I'm having is that although I can use time.localtime and get a 
> tuple of the year, month, day and so forth, I don't believe I can just minus 
> 1 from the day, because I don't think it's cyclic, also, I can't see the 
> date being linked in with the month.
>
> So is there any way of getting yesterdays date?
>
> Thank You
>
> Dominic 
>
>
>   
Here's how I'd do it:


>>> import time
>>> secondsPerDay = 24*60*60
>>> today = time.time()
>>> yesterday = today - secondsPerDay
>>> print time.strftime("%d%m%Y",time.localtime(today))
16082007
>>> print time.strftime("%d%m%Y",time.localtime(yesterday))
15082007

Gary Herron





More information about the Python-list mailing list