convert time

Chris Rebert clp2 at rebertia.com
Sun Sep 11 01:02:37 EDT 2011


2011/9/10 守株待兔 <1248283536 at qq.com>:
> how can i convert "Dec 11" into  2011-12?

Read the fine manuals for the `time` or `datetime` modules.
http://docs.python.org/library/datetime.html

>>> from datetime import datetime
>>> datetime.strptime("Dec 11", "%b %y")
datetime.datetime(2011, 12, 1, 0, 0)
>>> datetime.strptime("Dec 11", "%b %y").strftime("%Y-%m")
'2011-12'

Note that this code depends on an appropriate locale setting for
parsing the month name abbreviations.

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list