Dates and times · 13 / 15 Exercise Hard

Duration between two moments

Objective

Create a Python program that computes the duration between two instants (date and time) and shows it in days, hours and minutes. Subtracting two datetime gives a timedelta from which you get total_seconds().

Instructions

  • Read an integer n and then n lines with the format YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM: start date and time and end date and time (the end is always later than or equal to the start).
  • Convert each instant with datetime.fromisoformat (joining the date and the time with a space) and compute the difference in seconds.
  • Break the seconds down with divmod into days (86 400 s), hours (3 600 s) and minutes, and show Duration: 1d 2h 15m.
Input
3
2026-03-15 09:30 2026-03-16 11:45
2026-01-01 00:00 2026-01-01 00:00
2026-02-28 23:00 2026-03-01 01:30
Output
Duration: 1d 2h 15m
Duration: 0d 0h 0m
Duration: 0d 2h 30m

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise