Search code examples
pythontimestampstrptime

What is the correct format string to parse "04.05.2003 00:00:00.000 GMT+0100" with strptime()?


I am trying to convert a weird format string timestamp to a datetime. This is what it looks like:

04.05.2003 00:00:00.000 GMT+0100

I am trying something like this, but it's not matching. I think I am just missing the expression for that GMT offset at the end.

datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y HH:MM.SS.sss')

Of course it spits out the format error

ValueError: time data '04.05.2003 00:00:00.000 GMT+0100' does not match format '%d.%m.%Y HH:MM.SS.sss'

What is the format string I need to use here?


Solution

  • You need format operators for the time, not HH:MM:SS, and you need a format operator for the timezone. The operator for a timezone offset is %z.

    datetime.datetime.strptime('04.05.2003 00:00:00.000 GMT+0100','%d.%m.%Y %h:%M:%s.%f GMT%z')