Converting a string to datetime in python [duplicate]
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
精彩评论