You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用strptime转换UTC时间戳字符串为datetime时出现ValueError格式不匹配问题求助

使用strptime转换UTC时间戳字符串为datetime时出现ValueError格式不匹配问题求助

各位好!我最近在测试把UTC时间戳字符串通过strptime转换成datetime对象的时候,一直碰到格式不匹配的ValueError,试了好几种调整都没搞定,想请大家帮我看看问题出在哪。

报错信息如下:

ValueError: time data '2025-03-12T23:00:00.000Z' does not match format '%Y-%m-%dT%H:%M:%S.%f%Z'

这是我写的简单测试代码:

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}')

麻烦各位帮我分析下,到底是格式字符串哪里写错了,还是有其他我没注意到的细节问题呀?

备注:内容来源于stack exchange,提问作者D Chase

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 18:58:08