I am using this code to generate a range of hourly datetime
's:
from datetime import datetime, timedelta
def daterange(start_date, end_date):
# timedelta only has days and seconds attributes
for n in range(int ((end_date - start_date).seconds/3600 + 1)):
yield start_date + timedelta(n)
start_date = datetime(2013, 1, 1, 14, 00)
end_date = datetime(2015, 6, 2, 5, 00)
for single_date in daterange(start_date, end_date):
print single_date.strftime("%Y-%m-%d %H:%M")
But I am getting an unexpected result:
2013-01-01 14:00
2013-01-02 14:00
2013-01-03 14:00
2013-01-04 14:00
2013-01-05 14:00
2013-01-06 14:00
2013-01-07 14:00
2013-01-08 14:00
2013-01-09 14:00
2013-01-10 14:00
2013-01-11 14:00
2013-01-12 14:00
2013-01-13 14:00
2013-01-14 14:00
2013-01-15 14:00
2013-01-16 14:00
How can I better specify an hourly interval with the timedelta
seconds attribute? I'd rather not use external packages like NumPy.