How To Convert "thu Jun 5 10:59:10 Cdt 2014" Into Python Datetime Object?
How to convert 'Thu Jun 5 10:59:10 CDT 2014' into python datetime object? I can't seem to get it to work due to the CDT. %Z for that is throwing errors :(
Solution 1:
You can't use usual methods from datetime
to construct the object here. As mentioned in the documentation, strptime
can work only with (empty), UTC, EST, CST
:
>>> datetime.datetime.strptime("Thu Jun 05 10:59:10 UTC 2014", "%a %b %d %H:%M:%S %Z %Y")
datetime.datetime(2014, 6, 5, 10, 59, 10)
>>> datetime.datetime.strptime("Thu Jun 05 10:59:10 CDT 2014", "%a %b %d %H:%M:%S %Z %Y")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'Thu Jun 05 10:59:10 CDT 2014' does not match format'%a %b %d %H:%M:%S %Z %Y'
You need to take a look at python-dateutil
, which will parse the object into a naive datetime object (it does not take into account the timezone...):
>>>from dateutil import parser>>>parser.parse("Thu Jun 5 10:59:10 CDT 2014")
datetime.datetime(2014, 6, 5, 10, 59, 10)
Post a Comment for "How To Convert "thu Jun 5 10:59:10 Cdt 2014" Into Python Datetime Object?"