Python Program to Find the Number of Days Between Two Dates
Beginner · Dates and times
This program finds how many days there are between two dates. Python's date objects can be subtracted, and the result, called a timedelta, holds the gap in days. The program also splits the gap into weeks and days.
Program
From: 15 Jan 2026 To: 30 Mar 2026 Days between: 74 That is 10 weeks and 4 days Days including both dates: 75
How it works
date(2026, 1, 15)creates a date from a year, a month and a day.end - startgives atimedeltaobject, and its.daysattribute is the number of days between the dates, 74 here.- Python knows how long every month is, leap years included, so February 2026 is counted as 28 days.
//and%split the days into whole weeks and the days left over.- If the second date is earlier than the first, the answer is negative; wrap it in
abs()to always get a positive gap.
Other ways to write it
Countdown to an event
134 days left until the exam
The sign of the gap tells you whether the date is still to come, is today, or has already passed.
Dates typed as text
Days between 15-01-2026 and 30-03-2026 = 74
strptime() (string parse time) reads a date written as text, using the same codes as strftime(), which is how you turn typed input into a date.
Try it yourself
Find the number of days between 1 January 2028 and 31 December 2028, and see what the leap year changes.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises