datetime.不支持Strptime转换未来日期?



我在2022年11月13日有以下错误

end = datetime.strptime("2022-11-16", "%Y-%d-%m")
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/_strptime.py", line 352, in _strptime
raise ValueError("unconverted data remains: %s" %
ValueError: unconverted data remains: 6

,但我没有错误,在以下情况下,

date_value = datetime.strptime("2022-11-12", "%Y-%d-%m")

好吧,datetime模块的strptime函数不支持转换为未来日期?如果没有,请告诉我怎么走....

未来日期由datetime.strptime支持。这里,您试图转换16个月的日期,这导致了错误。我猜你在找

datetime.strptime("2022-11-16", "%Y-%m-%d")
datetime.strptime("2022-11-16", "%Y-%d-%m")

该格式字符串是year-day-month,但是您传递的日期字符串是year-month-day。因此,它试图将16解释为月份。

你的第二个例子碰巧工作,因为它有12个月。

Datetime strptime支持未来日期。我们只有12个月,但你要16个月,这根本不存在。

datetime.strptime("2022-16-11", "%Y-%d-%m")

最新更新