In this thread we have a good solution How to calculate the time interval between two time strings
But how I can edit the output format? From H:MM:SS to HH:MM, like 00:40 instead of 0:40:00
from datetime import datetime
from datetime import timedelta
T1 = input()
T2 = input()
format = '%H:%M'
tdiff = datetime.strptime(T1, format) - datetime.strptime(T2, format)
if tdiff.days < 0:
tdiff = timedelta(days = 0,
seconds = tdiff.seconds, microseconds = tdiff.microseconds)
print(tdiff)
EDIT: Thank you. This code below quite works, but the only problem is 7:45 instead of 07:45 for example. The code below removes the problem of seconds at the end. Now I only don't know how to force 0 is front of <10 hours format.
from datetime import datetime
from datetime import timedelta
T1 = input()
T2 = input()
format = '%H:%M'
tdiff = datetime.strptime(T1, format) - datetime.strptime(T2, format)
if tdiff.days < 0:
tdiff = timedelta(days = 0,
seconds = tdiff.seconds, microseconds = tdiff.microseconds)
print(str(tdiff).rstrip("0").rstrip(":"))