How to Calculate the Number of Days Between Two Dates in Python?

In this tutorial, I will explain how to calculate the number of days between two given dates in Python. As a Python developer in New York working on a project, I had a scenario to calculate the number of days overdue for each invoice by finding the days between the due date and the current date. I explored more about this topic and I will share my findings in this article.

Methods to Calculate the Number of Days Between Two Dates in Python

Python provides a couple of simple ways to calculate the number of days between two dates using the built-in datetime module. I’ll walk through a full example to demonstrate the process.

Read Python / vs //

Method 1: Subtract Two datetime Objects

The most direct way to find the number of days between two dates in Python is to simply subtract two datetime objects. When you subtract one datetime from another, the result is a timedelta object representing the time difference.

Here’s an example:

from datetime import datetime

date1 = datetime(2022, 4, 1) 
date2 = datetime(2022, 5, 15)

days_between = date2 - date1
print(days_between.days)

Output:

44

I have executed the above example and added the screenshot below.

Calculate the Number of Days Between Two Dates in Python

Explanation:

  1. First, we import the datetime class from the datetime module. This gives us access to the functionality for working with dates.
  2. Next, we create two datetime objects representing the two dates we want to find the difference between. The datetime constructor takes the year, month, and day as integer arguments.
  3. To calculate the number of days, we simply subtract date1 from date2. This returns a timedelta object.
  4. The timedelta object has a days attribute which gives us the number of days between the two dates as an integer. We print this out to get our final result of 44 days.

It’s important to note that a timedelta represents an exact difference in time, not just the date difference. So if date 2 had a different time than date 1, that would be reflected in the days calculation.

For example:

from datetime import datetime 

date1 = datetime(2022, 4, 1, 10, 0, 0)
date2 = datetime(2022, 4, 2, 16, 0, 0) 

days_between = date2 - date1
print(days_between.days)  # 1 
print(days_between)  # 1 day, 6:00:00

Here the full timedelta is 1 day and 6 hours, but days_between.days truncate that to just 1 day. Keep this behavior in mind depending on your use case.

Check out How to Use the repeat() Function in Python?

Method 2: Use the relativedelta Function

For more control over relative date comparisons, we can use the relativedelta function from the python-dateutil package. This third-party module extends the functionality of the standard datetime module.

Here’s how to install python-dateutil using pip:

pip install python-dateutil

Here’s an example of finding the days between dates with relativedelta:

from datetime import date
from dateutil.relativedelta import relativedelta

date1 = date(2022, 1, 1)
date2 = date(2022, 3, 31) 

difference_in_days = relativedelta(date2, date1).days
print(difference_in_days)

Output:

30

I have executed the above example and added the screenshot below.

How to Calculate the Number of Days Between Two Dates in Python

Explanation:

  1. First, we import the date class from datetime and the relativedelta function from dateutil.
  2. We create two date objects representing January 1, 2022, and March 31, 2022. Note that we use the date class here instead of the datetime class. The date class does not include time information.
  3. To get the relative difference between the dates, we pass them to relativedelta with the later date first. This returns a relativedelta object.
  4. The days attribute of the relativedelta object gives us the difference between the dates in days.

The relativedelta function also supports more specific relative date calculations. For example, you can find the difference in years, months, and days:

from datetime import date 
from dateutil.relativedelta import relativedelta

date1 = date(2020, 4, 1)
date2 = date(2022, 6, 15)

difference = relativedelta(date2, date1)
print(f"{difference.years} years, {difference.months} months, {difference.days} days")

Output:

2 years, 2 months, 14 days  

This level of granularity can be useful for generating human-readable output in certain applications.

Read How to Use the ceil() Function in Python?

Handle Leap Years and DST

When calculating date differences, it’s important to consider leap years and daylight saving time (DST) changes, especially if your application needs to handle dates spanning across them.

Python’s datetime and relativedelta implementations handle these cases properly without any extra code required.

For example, let’s find the number of days between January 1 and March 1 in both a leap year (2020) and a non-leap year (2021):

from datetime import date

leap_year_jan_1 = date(2020, 1, 1) 
leap_year_mar_1 = date(2020, 3, 1)

non_leap_year_jan_1 = date(2021, 1, 1)
non_leap_year_mar_1 = date(2021, 3, 1)  

print(f"2020 (leap year): {(leap_year_mar_1 - leap_year_jan_1).days} days")
print(f"2021 (non-leap year): {(non_leap_year_mar_1 - non_leap_year_jan_1).days} days")

Output:

2020 (leap year): 60 days
2021 (non-leap year): 59 days

I have executed the above example and added the screenshot below.

Calculate the Number of Days Between Two Dates in Python Handle Leap Years

The code correctly calculates the 60 days in January and February 2020 (a leap year) versus the 59 days in the same period in 2021.

Similarly, datetime handles DST transitions seamlessly. Let’s look at an example of the DST change in the United States in 2022:

from datetime import datetime

before_dst_change = datetime(2022, 3, 12, 12, 0, 0)  
after_dst_change = datetime(2022, 3, 14, 12, 0, 0)

print(f"Days between: {(after_dst_change - before_dst_change).days}")

Output:

Days between: 2

The DST change occurred on March 13, 2022, in most of the US, but datetime still correctly calculates the 2-day difference without any issues.

Check out How to Use the Python pop() Function?

Conclusion

In this tutorial, I have explained how to calculate the number of days between two given dates in Python. We discussed various ways to achieve this task, subtracting two datetime objects, and using the relativedelta function and we saw how to handle leap years and DST.

You may also like to read.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.