Python Practice › Basic Programs › Program 22
Days between two dates
Write a Python program to calculate the number of days between two dates.
Input
(2023, 1, 5) and (2023, 1, 22)
Output
17 days
Approach
- Create two
dateobjects. - Subtract them to get a
timedeltaand read its.days.
Solution
from datetime import date start = date(2023, 1, 5)end = date(2023, 1, 22)gap = end - startprint(gap.days, "days")Output
17 days
Line by line
Line 5
Subtracting dates gives a timedelta object.
Line 6
.days is the whole number of days between them.
Learn the concept
New to this topic? Read the lesson, then come back.