Re: date/time

From: David M. Cooke (cookedm+news_at_physics.mcmaster.ca)
Date: 01/06/05


Date: Wed, 05 Jan 2005 18:33:31 -0500

Thomas Guettler <guettli@thomas-guettler.de> writes:

> Am Wed, 05 Jan 2005 15:08:37 +0100 schrieb Nader Emami:
>
>> L.S.,
>>
>> Could somebody help me how I can get the next format of date
>> from the time module?
>
> I don't understand your question. Do you want to have the next day?
>
> 20041231 --> 20050101 ?
>
> You can do it like this:
> - parse the string with time.strptime
> - timetuple[2]+=1
> - mktime(timetuple) # --> secs
> - strftime(localtime(secs))

Or using the datetime module:

import time, datetime

tt = time.strptime('20041231', '%Y%m%d')
t = datetime.date.fromtimestamp(time.mktime(tt))
# now in a easier-to-handle form than the time tuple
t += datetime.timedelta(days=1)
print t.strftime('%Y%m%d')

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca


Relevant Pages

  • Re: Filename type (Was: Re: finding file size)
    ... the second step is deprecating the old way, ... surely not be made until Python 3.0. ... I would like to see a datetime module that makes the time module ...
    (comp.lang.python)
  • Re: Time
    ... HS ==> I need to convert the string below into epoch seconds so that I ... perform substractions and additions. ... If you are desperate to use the time module, ... Do read the datetime module documentation for more info ... ...
    (comp.lang.python)
  • Re: date/time
    ... On 2005-01-05, Nader Emami wrote: ... > from the time module? ... > attributes of format %Y%m%d. ... I would use the datetime module: ...
    (comp.lang.python)
  • Re: days since epoch
    ... how can i get the number of days since epoch using the time module? ... Try the datetime module -- better suited to computing days, ...
    (comp.lang.python)
  • Re: Day of Year Behaviour
    ... > I'm using the time module without success: ... localtime) should normalise the date correctly: ... But, as others have stated, the datetime module is probably the better way ...
    (comp.lang.python)