python
datetime
1 year, 2 months ago
from datetime import datetime, timedelta
def get_hours(tm1, tm2, start_date):
dates_hours = {}
start = datetime.strptime(start_date, "%Y-%m-%d")
end = tm2.strftime("%Y-%m-%d")
if start == end:
return {start: (tm2-tm1).total_seconds()/60}
dates_hours[start_date] = ((tm1 - start).total_seconds())/60
days = (tm2 - tm1).days
for d in range(days):
dt = start + timedelta(days=d+1)
dates_hours[dt.strftime("%Y-%m-%d")] = 24*60
end_date = datetime(tm2.year, tm2.month, tm2.day)
dates_hours[end] = ((tm2 - end_date).total_seconds())/60
return dates_hours
dt1 = datetime.strptime("2023-10-21 08:30", "%Y-%m-%d %H:%M")
dt2 = datetime.strptime("2023-10-22 09:30", "%Y-%m-%d %H:%M")
print(get_hours(dt1, dt2, '2023-10-21'))
0 Comments
Please Login to Comment Here