python - Strptime error: Value Error: Time data does not match format - Stack Overflow

I am getting an error message when testing converting a UTC timestamp string to datetime using strptime

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
Add a comment  | 

1 Answer 1

Reset to default 2

Remove 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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744691080a4588213.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信