Reputation: 339
I want to calculate the time difference in hour not considering the date.
For example I want the difference between 22:00:00
and 01:00:00
to be 3
.
The code I got so far:
time1 = datetime.strptime("22:00:00", '%H:%M:%S')
time2 = datetime.strptime("01:00:00", '%H:%M:%S')
res = time1-time2
print(res)
The output I got: 21:00:00
The output I want: 3
Upvotes: 1
Views: 70
Reputation: 88236
You cannot extract the hours directly from the resulting timedelta
object, as they keep fraction-of-day as seconds, so a simple workaround is to take the seconds and calculate the hours from there.
Also note that you want to subtract time1
to time2
, otherwise you will get 21
hours:
(time2 - time1).seconds/3600
# 3
Upvotes: 2