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

Parse Datetime with +0 timezone

I have the following Datetime string: Dec 03 2020 01: +0 which I want to parse into a datetime object.

dtObj = datetime.strptime("Dec 03 2020 01: +0", '%b %d %Y %I: %z')

Checking the Documentation, this should work but I get the following error:

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

ValueError: time data 'Dec 03 2020 01: +0' does not match format '%b %d %Y %I: %z'

Any ideas what I have overseen?

Thanks in advance

>Solution :

Any ideas what I have overseen?

strftime.org claims that %z

UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object
is naive).

this mean that it must contain at least 4 digits after + or - (HHMM part, which is compulsory), taking this is account Dec 03 2020 01: +0 is not compliant with used format string, whilst Dec 03 2020 01: +0000 is

import datetime
dtObj = datetime.datetime.strptime("Dec 03 2020 01: +0000", '%b %d %Y %I: %z')
print(dtObj)

gives output

2020-12-03 01:00:00+00:00
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