datetime.datetime + timedelta(days=5)返回None


startdate = dt.strptime('13/12/2020', '%d/%m/%Y')
enddate = dt.strptime('4/12/2020', '%d/%m/%Y')
interval = 5
print('startdate', startdate)
print('enddate', enddate)
loop_startdate = startdate
loop_endate = None
for _ in range((diff//interval) + 1):
print(_, loop_startdate)
loop_enddate = loop_startdate + timedelta(days=interval)
print('loop_enddate', loop_endate)
if loop_enddate > enddate:
loop_enddate = enddate
loop_startdate = loop_endate

startdateenddatedatetime.datetime实例。我已经打印出'startdate'和enddate,结果如下:

startdate 2020-12-04 00:00:00
enddate 2020-12-13 00:00:00
0 2020-12-04 00:00:00
loop_enddate None
1 None
Traceback (most recent call last):
File "pythonfilepathhere", line 81, in <module>
loop_enddate = loop_startdate + timedelta(days=interval)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'datetime.timedelta'

我已经仔细检查了,以确保startdateendate是在datetime.datetime类型。我需要它作为date而不是datetime。我也尝试使用loop_startdate.date() + timedelta(),但它也返回None。

我不明白为什么datetime.datetime + timedelta()返回None。我的Python版本是3.6.5

好像是个小错字。

print('loop_enddate', loop_endate)改为print('loop_enddate', loop_enddate)

同时,将loop_startdate = loop_endate更改为loop_startdate = loop_enddate

相关内容

  • 没有找到相关文章

最新更新