Reputation: 101
I have only found question Convert Days and Time (Hours x Minutes x Seconds) to Time only on stackoverflow and that seems like it would help me out but it doesn't seem to totally apply to what I'm doing.
I'm writing a wage tracking program and need it to give me the total sum of all hour input. I've got a much smaller and abridged form of it to just work on this one aspect. It saves a lot of time as the main program requires all the individual start and end times to be input by the user. It is currently showing 2 days, 22:00:00
as on output whereas I ideally would prefer it to show 70:00
, showing only the hours and minutes and getting rid of the unneeded second part.
import datetime
def time_diff(a, b): # Calculates time difference between start and finish
return b - a
start = '10:00'
mon_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
mon_fin = datetime.datetime.strptime(finish, '%H:%M')
mon_hours = time_diff(mon_start, mon_fin)
start = '10:00'
tue_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
tue_fin = datetime.datetime.strptime(finish, '%H:%M')
tue_hours = time_diff(tue_start, tue_fin)
start = '10:00'
wed_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
wed_fin = datetime.datetime.strptime(finish, '%H:%M')
wed_hours = time_diff(wed_start, wed_fin)
start = '10:00'
thu_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
thu_fin = datetime.datetime.strptime(finish, '%H:%M')
thu_hours = time_diff(thu_start, thu_fin)
start = '10:00'
fri_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
fri_fin = datetime.datetime.strptime(finish, '%H:%M')
fri_hours = time_diff(fri_start, fri_fin)
start = '10:00'
sat_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
sat_fin = datetime.datetime.strptime(finish, '%H:%M')
sat_hours = time_diff(sat_start, sat_fin)
start = '10:00'
sun_start = datetime.datetime.strptime(start, '%H:%M')
finish = '20:00'
sun_fin = datetime.datetime.strptime(finish, '%H:%M')
sun_hours = time_diff(sun_start, sun_fin)
total_hours = mon_hours + tue_hours + wed_hours + thu_hours + fri_hours + sat_hours + sun_hours
print(total_hours)
Upvotes: 0
Views: 645
Reputation: 139
All the above solutions will work, but just wanted to share something from my side as well.
Code:
seconds = time_diff(a, b).total_seconds()
def convert(seconds):
mins, secs = divmod(int(seconds), 60)
hours, mins = divmod(mins, 60)
return f'{hours:02d}:{mins:02d}:{secs:02d}'
print(convert(seconds)) # Will print in HH:MM:SS format
Upvotes: 0
Reputation: 905
Just use this print at the end:
print(
"{}:{:02d}".format(
total_hours.days * 24 + total_hours.seconds // 3600,
(total_hours.seconds // 60) % 60,
)
Upvotes: 1
Reputation: 36340
This:
def time_diff(a, b): # Calculates time difference between start and finish
return b - a
if a
is datetime.datetime
and b
is datetime.datetime
return
datetime.timedelta
object. You can use its .total_seconds
method to get value in second, which then you might use for further calculations, for example
import datetime
total = datetime.timedelta(days=3,minutes=10)
total_minutes = int(total.total_seconds()//60)
hours, minutes = total_minutes // 60, total_minutes % 60
print(hours, minutes) # 72 10
Upvotes: 2