Difference between two times

EasyDatetime Programs · 12 of 20

Write a Python program to find hours and minutes between two datetimes.

Advertisement
Input
09:15 to 17:40
Output
8h 25m

Approach

  1. Subtract and split the seconds.

Solution

Python
from datetime import datetime start = datetime(2026, 11, 2, 9, 15)end = datetime(2026, 11, 2, 17, 40)minutes = int((end - start).total_seconds() // 60)print(f"{minutes // 60}h {minutes % 60}m")
Output
8h 25m

Learn the concept

New to this topic? Read the lesson, then come back.

More datetime programs

← All 20 Datetime Programs

Leave a Comment