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

How do I convert type of a datetime string which has Z in it

I have a datetime string like this: 2021-11-13T09:20:15.497Z, I’m just wondering how can I convert this into a datetime object?
I tried datetime.datetime.fromiso() but it told me that the format is not an iso format
Thanks in advance!

>Solution :

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

Use "%Y-%m-%dT%H:%M:%S.%f%z" format

from datetime import datetime

x = datetime.strptime("2021-11-13T09:20:15.497Z", "%Y-%m-%dT%H:%M:%S.%f%z")
print(x)              # 2021-11-13 09:20:15.497000+00:00
print(x.isoformat())  # 2021-11-13T09:20:15.497000+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