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:
Similar Reads
Python program to find day of the week for a given date To find the day of the week for a given date, we convert the date into a form that Python can understand and then determine which weekday it falls on. For example, the date "22 02 2003" corresponds to a Saturday.Letâs explore simple and efficient ways to do this.Using Zellerâs CongruenceZellerâs Con
3 min read
Python Program to Get total Business days between two dates Given two dates, our task is to write a Python program to get total business days, i.e weekdays between the two dates. Example: Input : test_date1, test_date2 = datetime(2015, 6, 3), datetime(2015, 7, 1) Output : 20 Explanation : Total weekdays, i.e business days are 20 in span. Input : test_date1,
6 min read
Python program to find the first day of given year Given a year as input, write a Python to find the starting day of the given year. We will use the Python datetime library in order to solve this problem. Example: Input : 2010 Output :FridayInput :2019 Output :Tuesday Below is the implementation: Python3 # Python program to find the first day of giv
2 min read
Python program to find Last date of Month Given a datetime object, the task is to write a Python Program to compute the last date of datetime object Month. Examples: Input : test_date = datetime.datetime(2018, 6, 4) Output : 30 Explanation : April has 30 days, each year Input : test_date = datetime.datetime(2020, 2, 4) Output : 29 Explanati
3 min read
Python - Convert day number to date in particular year Given day number, convert to date it refers to. Input : day_num = "339", year = "2020" Output : 12-04-2020 Explanation : 339th Day of 2020 is 4th December. Input : day_num = "4", year = "2020" Output : 01-04-2020 Explanation : 4th Day of 2020 is 4th January. Method #1 : Using datetime.strptime() In
5 min read
Python program to get the nth weekday after a given date Given a date and weekday index, the task is to write a Python program to get the date for the given day of the week occurring after the given date. The weekday index is based on the below table: IndexWeekday0Monday1Tuesday2Wednesday3Thursday4Friday5Saturday6Sunday Examples: Input : test_date = datet
3 min read