easy +10 pts

Time Difference HH:MM

Compute the positive difference between two times in HH:MM format.

Write a function `time_difference(time1: str, time2: str) -> str` that accepts two strings representing times in 24-hour HH:MM format (e.g., "09:30"). The function should return the absolute difference between the two times as a string in the same HH:MM format, with two digits for hours and two digits for minutes. For example, the difference between "09:30" and "11:15" is 1 hour and 45 minutes, so the output is "01:45". The inputs are always valid times between "00:00" and "23:59". The output hours will be between "00" and "23" (since the maximum difference is less than 24 hours).

Constraints

Inputs are valid 24-hour times in HH:MM format. Hours are two digits from 00 to 23, minutes two digits from 00 to 59. The function must return a string in HH:MM format with exactly two digits for hours and two digits for minutes.

Example

>>> time_difference("09:30", "11:15")
'01:45'
>>> time_difference("23:00", "01:00")
'22:00'
>>> time_difference("00:00", "00:00")
'00:00'
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Parse each time into minutes since midnight: hours * 60 + minutes.
Use absolute difference in minutes and convert back to hours and minutes.
Format hours and minutes with leading zeros using f"{h:02d}:{m:02d}".
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.