如何查找日期範圍內的小時數?我目前正在計算和打印兩個給定日期之間的日期。計算Python中日期之間的小時數範圍
from datetime import timedelta as td, datetime
d1 = datetime.strptime(start_date, '%Y-%m-%d')
d2 = datetime.strptime(end_date, '%Y-%m-%d')
def get_delta(d1, d2):
delta = d2 - d1
return delta
delta = get_delta(d1,d2)
for i in range(delta.days + 1):
print d1 + td(days=i)
電流輸出:
2016-04-22
2016-04-23
2016-04-24
2016-04-25
2016-04-26
2016-04-27
2016-04-28
...
預期輸出:
2016-04-22 00:00:00
2016-04-22 01:00:00
2016-04-22 02:00:00
2016-04-22 03:00:00
2016-04-22 04:00:00
2016-04-22 05:00:00
2016-04-22 06:00:00
2016-04-22 07:00:00
...
什麼是get_delta()? d1和d2是datetime.datetime對象還是datetime.date對象? – kingledion