Convert Date to Short formate

Fredrik Lundh fredrik at pythonware.com
Wed Jun 7 22:33:25 CEST 2006


aqmaiya wrote:

>  there is string value 'Dec 06, 2000' I want to convert that string
> date to SHORT FORMAT like '2000-12-06-. Please help me how do I do
> that?

you mean ISO format, right?

the easiest way is probably to use the time.strptime module to parse the 
original string into a time tuple, and then use time.strftime to format 
the tuple as an ISO date:

 >>> s = "Dec 06, 2000"
 >>> t = time.strptime(s, "%b %d, %Y")
 >>> t
(2000, 12, 6, 0, 0, 0, 2, 341, -1)
 >>> time.strftime("%Y-%m-%d", t)
'2000-12-06'

also see:

     http://docs.python.org/lib/module-time.html

</F>




More information about the Python-list mailing list