I am getting an error message when testing converting a UTC timestamp string to datetime using strptime.
ValueError: time data '2025-03-12T23:00:00.000Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%Z'
Here is a simple test that
from datetime import datetime, timezone
utc_string = "2025-03-12T23:00:00.000Z"
print(f'Time in: {utc_string}')
time_utc = datetime.strptime(utc_string, '%Y-%m-%dT%H:%M:%S.%f%Z').replace(tzinfo=timezone.utc)
print(f'Time out: {time_utc}')
I am getting an error message when testing converting a UTC timestamp string to datetime using strptime.
ValueError: time data '2025-03-12T23:00:00.000Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%Z'
Here is a simple test that
from datetime import datetime, timezone
utc_string = "2025-03-12T23:00:00.000Z"
print(f'Time in: {utc_string}')
time_utc = datetime.strptime(utc_string, '%Y-%m-%dT%H:%M:%S.%f%Z').replace(tzinfo=timezone.utc)
print(f'Time out: {time_utc}')
Share
Improve this question
asked Mar 13 at 16:13
D ChaseD Chase
14711 bronze badges
0
1 Answer
Reset to default 2Remove the '%' before Z, Z does not need the %:
time_utc = datetime.strptime(utc_string, '%Y-%m-%dT%H:%M:%S.%fZ').replace(tzinfo=timezone.utc)