Finding elapsed time across midnight without dates

62 Views Asked by At

How would I compute the elapsed time between time1 = 23:30:00 and time2 = 01:30:00? They do not have dates associated with them, but python attaches the date 1900-01-01 when I make them into datetime objects, so it would become time1 = 1900-01-01 23:30:00 and time2 = 1900-01-01 01:30:00. My desired outcome would be 2 hours.

1

There are 1 best solutions below

0
On BEST ANSWER

you can subtract 2 datetime objects and get the difference between them

from datetime import datetime

# d = datetime(year, month, day, hour, min, sec)
d1 = datetime(2017, 9, 1, 23, 30, 00)
d2 = datetime(2017, 9, 2, 1, 30, 00)
print d2 - d1

2:00:00