Reputation: 1
I am trying to convert a string into date type format but stuck with the error
valueError: time data '2021-02-01T04:10:39.669Z' does not match format '%Y-%m-%dT%M:%H:%S%f%z'
I am sharing my code. Any help will be appreciable. Thanks in Advance.
from datetime import datetime
date_string = "2021-02-01T04:10:39.669Z"
format = "%Y-%m-%dT%M:%H:%S%f%z"
created_datetime = datetime.strptime(date_string, format)
print(created_datetime)
Upvotes: 0
Views: 768
Reputation: 3121
Your format has two errors. You must use the first hour then the minute. Now Since your timezone is started from dot so use .%f
and timezone z
with together .%fz
. If you don't aware of the timezone then use always capital Z
from datetime import datetime
date_string = "2021-02-01T04:10:39.669Z"
created_datetime = datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ')
print(created_datetime)
# Output
# 2021-02-01 04:10:39.669000
Upvotes: 3