开发者

ValueError when using strptime to get a datetime object [duplicate]

This question already has answers here: Convert timestamps with offset to datetime obj using strptime (4 answers) 开发者_高级运维 Closed 7 years ago.

Im trying to convert a date string to a datetime object as shown below :

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

But,im getting the error below :

Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.6/_strptime.py", line 328, in _strptime data_string[found.end():]) ValueError: unconverted data remains: +00:00

I guess there is a problem with my format string. How to fix that ?

Thank You


How about ...

    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)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜