【发布时间】:2013-02-13 18:59:37
【问题描述】:
我在 JSON 网络服务中使用默认的 python 日期时间字符串格式。
然后,我尝试将其与实际日期时间进行比较。我也在使用pytz.utc 的时区。
这是我的字符串日期:
print date
2013-02-26 21:28:37.261134+01:00
尝试将我的字符串转换为日期时间(使用 pytz 编辑时区):
if datetime.strptime(date, '%Y-%m-%d %H:%M:%S.%f+%Z') < datetime.now(pytz.utc):
不幸的是,它不起作用。
ValueError: time data '2013-02-26 21:28:37.261134+01:00' does not match format '%Y-%m-%d %H:%M:%S.%f%Z'
谁能告诉我strptime 格式的正确语法,以使用我的日期?
【问题讨论】:
-
您不能将感知时区与
datetime.now()进行比较,您也必须给后者一个时区。见How to make an unaware datetime timezone aware in python -
你说得对,谢谢。谷歌什么也没给我......对于时区,我实际上在我的代码中使用了
datetime.now(pytz.utc)。对不起,误导......