开发者

Converting a string to datetime in python [duplicate]

This question already has answers here: Closed 11 year开发者_如何学运维s ago.

Possible Duplicate:

Parse date and format it using python?

i have a date string like 2011-07-15 13:00:00+00:00 . Any way to convert this string to a datetime object in python ?

Thank You


You can handle the time zone appropriately, and have the format recognized automatically, if you use dateutil.parser, as described here:

from dateutil import parser
dt = parser.parse("2011-07-15 13:00:00+00:00")


You can use the strptime function in the datetime module. http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior

datetime.datetime.strptime('2011-07-15 13:00:00', '%Y-%m-%d %H:%M:%S')

Edit:

Lesson learnt, the datetime module isn't capable of doing this, at least on my platform. Mu Mind's solution seems to be the easiest way to do this robustly according to http://wiki.python.org/moin/WorkingWithTime

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜