使用json d检查过期日期时出现问题

2024-09-29 01:19:49 发布

您现在位置:Python中文网/ 问答频道 /正文

使用json日期检查过期日期时出现问题

我哪里出错了

from datetime import datetime, date
datet = '2019-06-12T00:00:00+00:00'

ExpirationDate = datetime.strptime(datet, "%Y-%m-%dT%H:%M:%S.%f")
now = datetime.now()
if ExpirationDate >= now:
  print("ok")
Traceback (most recent call last):   File "main.py", line 4, in <module>
    ExpirationDate = datetime.strptime(datet, "%Y-%m-%dT%H:%M:%S.%f")   File "/usr/local/lib/python3.6/_strptime.py", line 565, in
_strptime_datetime
    tt, fraction = _strptime(data_string, format)   File "/usr/local/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format)) ValueError: time data '2019-06-12T00:00:00+00:00' does not matchformat '%Y-%m-%dT%H:%M:%S.%f'

Tags: inpyformatdatadatetimestringlibusr
1条回答
网友
1楼 · 发布于 2024-09-29 01:19:49

你的问题在最后的+00:00。在格式字符串中,它匹配到.%f。您必须将格式字符串更改为包含+,或者将日期改为.00,而不是+00:00

相关问题 更多 >