Home > Net >  Python how to convert string with special character into datetime
Python how to convert string with special character into datetime

Time:11-12

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.

Any friend can help?

CodePudding user response:

Your format string is incorrect.


>>> datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2021, 7, 7, 2, 27)
  • Related