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

Python how to convert string with special character into datetime

Trying to convert a string to datetime:

from datetime import datetime

date_string = "2021-07-07T02:27:00Z"

datetime.strptime('2021-07-07T02:27:00Z', '%Y-%M-%d %H:%M:%S')


Error:
error: redefinition of group name 'M' as group 5; was group 2 at position 107

Any then I tried:

datetime.fromisoformat(date_string)

Still not working.

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

Any friend can help?

>Solution :

Your format string is incorrect.


>>> datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2021, 7, 7, 2, 27)
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