Python Practice › Datetime Programs › Program 12
Difference between two times
Write a Python program to find hours and minutes between two datetimes.
Input
09:15 to 17:40
Output
8h 25m
Approach
- Subtract and split the seconds.
Solution
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.