Editor

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

days_between_dates.py
from datetime import date
start = date(2026, 1, 15) # or: start = date.today()
end = date(2026, 3, 30) # or: end = date(int(input("Year: ")), int(input("Month: ")), int(input("Day: ")))
gap = end - start # subtracting two dates gives a timedelta
print("From:", start.strftime("%d %b %Y"))
print("To: ", end.strftime("%d %b %Y"))
print("Days between:", gap.days)
print("That is", gap.days // 7, "weeks and", gap.days % 7, "days")
# Counting both the first and the last day
print("Days including both dates:", gap.days + 1)
Output
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

  1. date(2026, 1, 15) creates a date from a year, a month and a day.
  2. end - start gives a timedelta object, and its .days attribute is the number of days between the dates, 74 here.
  3. Python knows how long every month is, leap years included, so February 2026 is counted as 28 days.
  4. // and % split the days into whole weeks and the days left over.
  5. 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

Countdown to an event
from datetime import date
today = date(2026, 10, 18) # or: today = date.today()
exam = date(2027, 3, 1)
days_left = (exam - today).days
if days_left > 0:
print(days_left, "days left until the exam")
elif days_left == 0:
print("The exam is today!")
else:
print("The exam was", -days_left, "days ago")
Output
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

Dates typed as text
from datetime import datetime
first = "15-01-2026" # or: first = input("First date (dd-mm-yyyy): ")
second = "30-03-2026"
d1 = datetime.strptime(first, "%d-%m-%Y").date()
d2 = datetime.strptime(second, "%d-%m-%Y").date()
print("Days between", first, "and", second, "=", abs((d2 - d1).days))
Output
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.

All Python programs · CBSE class 12 practical file · Python exercises