时间数据'2019-06-02T16:19:27.000-04:00'与格式'%Y-%m-%dT%H:%M:%S.%fZ'不匹配”

问题描述 投票:2回答:2

我在执行操作时遇到上述错误:

datetime.strptime(item['dt'], "%Y-%m-%dT%H:%M:%S.%fZ")

即使我尝试:

datetime.strptime(item['dt'], "%Y-%m-%dT%H:%M:%S.%f")

我正在获取未转换的数据仍然:-04:00作为我的错误。

我在做什么错?

python mongodb datetime pymongo strptime
2个回答
0
投票
根据format codesdatetime.strptime,应改为使用%z表示UTC偏移量:

datetime.strptime('2019-06-02T16:19:27.000-04:00', "%Y-%m-%dT%H:%M:%S.%f%z")


0
投票
这是因为您的utc偏移量是04:00,而不是0400 ...请尝试:

datetime.datetime.strptime('2019-06-02T16:19:27.000-0400', "%Y-%m-%dT%H:%M:%S.%f%z")

输出:

datetime.datetime(2019, 6, 2, 16, 19, 27, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))

[%z正在期待类似hhmm

[https://docs.python.org/3/library/datetime.html-查看格式代码

© www.soinside.com 2019 - 2024. All rights reserved.