Search code examples
pythondatedatetimepython-dateutil

Parsing long string as datetime


When using

import datetime
s = 'Sat Apr 23 2016 00:00:00 GMT+0100'
print datetime.datetime.strptime(s, "%a %m %d %y %H:%M:%S GMT+0100")

I get:

ValueError: time data 'Sat Apr 23 2016 00:00:00 GMT+0100' does not match format '%a %m %d %y %H:%M:%S GMT+0100'

How to parse such a string?

Note: Using dateutil.parser.parse didn't work : it produced some weird datetime object that I could not subtract with another datetime, i.e. d1 - d2 didn't work.


Solution

  • According to this reference, the format should be "%a %b %d %Y %H:%M:%S GMT+0100"