Python program to find number of days between two given dates
Last Updated :
11 Jul, 2025
Finding the number of days between two dates means calculating how much time has passed from one date to another. This is helpful in real-life situations like tracking deadlines, calculating age, planning events or measuring durations in billing and scheduling.
Example:
Input: dt1 = 01/01/2004, dt2 = 01/01/2005
Output: 366 days
Python makes this easy through various methods. Let's explore them one by one.
Using datetime module
datetime module allows a program to easily calculate the number of days between two dates by subtracting one date object from another. It is a simple, built-in and reliable approach for handling date differences.
Example:
This code calculates and prints the number of days between two given dates using datetime module.
Python
from datetime import date
d1 = date(2023, 5, 10)
d2 = date(2025, 7, 10)
dif = d2 - d1
print(dif.days, "days")
Explanation:
- d1 and d2 are created as date objects representing May 10, 2023 and July 10, 2025.
- dif = d2 - d1 subtracts two dates resulting in a timedelta object.
- dif.days returns the number of days between the two dates.
Using time module
time module can calculate the number of days between two dates by converting them into timestamps and finding the difference in seconds then dividing by 86,400 (seconds in a day).
Example: Here, time module is used to find the number of days between two dates by converting them into timestamps.
Python
import time
d1 = "10/05/2023"
d2 = "10/07/2025"
# Define the date format
date_format = "%d/%m/%Y"
# Convert to time tuples, then to timestamps
t1 = time.mktime(time.strptime(d1, date_format))
t2 = time.mktime(time.strptime(d2, date_format))
days = int((t2 - t1) / 86400)
print(days, "days")
Explanation:
- date_format tells Python how to interpret the date strings.
- time.strptime() converts the date string into a time structure (tuple format).
- time.mktime() converts time structure to a Unix timestamp (in seconds).
Using dateutil.relativedelta
dateutil.relativedelta module allows programs to find difference between two dates in terms of years, months and days. It provides a more detailed and readable breakdown of the time gap.
Example: In this program dateutil.relativedelta is used to find the difference between two dates:
Python
from datetime import datetime
from dateutil.relativedelta import relativedelta
d1 = datetime(2023, 5, 10)
d2 = datetime(2025, 7, 10)
diff = relativedelta(d2, d1)
print(diff.years, "years,", diff.months, "months,", diff.days, "days")
Output2 years, 2 months, 0 days
Explanation: relativedelta(d2, d1) returns the difference as a relativedelta object, storing the gap in years, months and days.
reduce() applies a function to elements in a sequence. It can be used to find the number of days between two dates by subtracting them.
Example: This program uses reduce() to subtract two dates and find the number of days between them in a functional style.
Python
from functools import reduce
from datetime import date
dates = [date(2023, 5, 10), date(2025, 7, 10)]
days = reduce(lambda x, y: (y - x).days, dates)
print(days, "days")
Explanation:
- A list dates is created with two date objects: 2023-05-10 and 2025-07-10.
- reduce(lambda x, y: (y - x).days, dates) subtracts first date from second and returns difference in days.
Related Articles:
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice