Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading