Getting Number Of Hours Between A Date-time Range In Python
So I'm trying to print the total hours in intervals between a start date and an end date in python as follows: @app.route('/test/') def test(): date_format = '%Y-%m-%d %H:%M:%S' s
Solution 1:
You might be overthinking it.
from datetime import datetime, timedelta
date_format = "%Y-%m-%d %H:%M:%S"
start_date_time = datetime.strptime("2018-10-16 07:00:00", date_format)
end_date_time = datetime.strptime("2018-10-18 22:00:00", date_format)
def get_delta(l, r):
return abs(int((l-r).total_seconds())) / 3600
for h in range(int(get_delta(start_date_time, end_date_time))):
print((start_date_time + timedelta(0, h*3600)).strftime(date_format))
Post a Comment for "Getting Number Of Hours Between A Date-time Range In Python"