最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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)
发布评论

评论列表(0)

  1. 暂无评论