Python Practice › Datetime Programs › Program 19
Countdown to an event
Write a Python program that prints the days, hours and minutes until an event.
Output
59 days, 13 hours, 59 minutes
Approach
- Split a timedelta with
divmod.
Solution
from datetime import datetime now = datetime(2026, 11, 2, 10, 0)event = datetime(2026, 12, 31, 23, 59)left = event - nowhours, rest = divmod(left.seconds, 3600)print(f"{left.days} days, {hours} hours, {rest // 60} minutes")Output
59 days, 13 hours, 59 minutes
Learn the concept
New to this topic? Read the lesson, then come back.