我正在尝试将日期字符串转换为datetime对象:
dt = datetime.datetime.strptime('2011-07-15 13:00:00+00:00', '%Y-%m-%d %H:%M:%S')
但是我得到这个错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.6/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: +00:00
我猜我的格式字符串有问题。如何解决这个问题?
去掉偏移量怎么样?
dt_string = '2011-07-15 13:00:00+00:00'
new_dt = dt_string[:19]
dt = datetime.datetime.strptime(new_dt, '%Y-%m-%d %H:%M:%S')
dt = datetime.datetime.strptime('2011-07-15 13:00:00+00:00', '%Y-%m-%d %H:%M:%S%z')
>>> datetime.datetime.strptime('2011-07-15 13:00:00', '%Y-%m-%d %H:%M:%S'
datetime.datetime(2011, 7, 15, 13, 0)