parsing a date

Peter peter at
Sat Sep 24 01:40:58 CEST 2005

Kalle Anke wrote:

>I want to parse a date string, for example '2005-09-23', and since I haven't 
>done this before I would like to ask what is the best way to do it.
>I've looked around and the dateutil seems to be what most people use, but 
>unfortunately I only get an empty file when I try to download it. I also 
>tried the standard modules and ended up with this
>import datetime
>from time import strptime
>d = '2005-09-23'
>w = strptime(d,'%Y-%m-%d')
>print w[0], w[1], w[2] )
Well, thats not _to_ bad.

>But I suspect there is a better way to do it??
There is...

I would check out "strftime"
With strftime you could do something like this...

# ------
print strftime("%Y-%m-%d")
# -----

or if you wanted to get the time in the past or future you could do 
something like

# ------
tm = time.localtime(time.time() + ((60 * 60) * 24))
print strftime("%Y-%m-%d", tm)
# -----

Which gets the date of tommarow.


More information about the Python-list mailing list