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

converting json timestamp to datetime in python

How can I convert ‘2021-11-21T18:57:18.000+01:00’ from json to datetime in Python?

  timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")

is giving me error:
ValueError: time data ‘2021-11-21T18:57:18.000+01:00’
does not match format ‘%Y-%m-%dT%H:%M:%S.%fZ’

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

>Solution :

Your formatting string is slightly wrong. You need %z to parse the zone offset:

from datetime import datetime

timestamp = '2021-11-21T18:57:18.000+01:00'
timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f%z")

print(timestamp)

Result:

2021-11-21 18:57:18+01: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