In this article, we will discuss how to get the difference between two dates in days in python.

Python provides a module datetime for manipulation of date and time. It consists of following classes,

  • datetime.date : An object of date class specifies a date using year, month and day
  • datetime.time : An object of time class specifies a timestamp using hour, minute, second, microsecond, and tzinfo.
  • datetime.datetime: An object of datetime is a combination of a date and a time.
  • datetime.timedelta: A duration, that represents the difference between two dates or times or datetimes.
  • datetime.tzinfo: It contains the time zone information.

Now to get the difference between two dates in python, we will use the datetime module. First we will create start and end datetime objects from the string dates. Then we will subtract the datetime objects to get the duration as the datetime.timedelta object. Then using the properties of timedelta object, we will fetch the days in between the two dates. Let’s understand with some examples,

Python Example 1: Get difference between two dates in days

from datetime import datetime

date_1 = '2/7/2021'
date_2 = '24/7/2021'

start = datetime.strptime(date_1, "%d/%m/%Y")
end =   datetime.strptime(date_2, "%d/%m/%Y")

# get the difference between wo dates as timedelta object
diff = end.date() - start.date()

print('Difference between dates in days:')
print(diff.days)

Output:

Difference between dates in days:
22

The different between ’24/7/2021′ and ‘2/7/2021’ is 22 days

How did it work ?

We createed the two datetime objects from the two dates in the string format, by passing the date and it’s format in the strptime() function. Then we subtracted these two datetime objects and got the datetime.timedelta object, which represents the duration between two dates. The timedelta class has a member variable days, which represents the number of days in this duration. We used that fetch the difference between two dates in days.

Python Example 2: Get difference between two dates in days

Let’s checkout an another example, where both dates are more than 2 years apart,

from datetime import datetime

date_1 = '2/7/2021'
date_2 = '24/7/2023'

start = datetime.strptime(date_1, "%d/%m/%Y")
end =   datetime.strptime(date_2, "%d/%m/%Y")

# get the difference between two dates as timedelta object
diff = end.date() - start.date()

print('Difference between dates in days:')
print(diff.days)

Output:

Difference between dates in days:
752

The different between ’24/7/2023′ and ‘2/7/2021’ is 752 days

Python Example 3: Get difference between two datetimes in days

Let’s checkout an another example, where instead of date strings, we have complete timestamps in string and we want the difference between these two timestamps in days. For example,

from datetime import datetime

date_1 = '24/7/2021 11:13:08.230010'
date_2 = '1/9/2023 11:14:18.333338'

date_format_str = '%d/%m/%Y %H:%M:%S.%f'

start = datetime.strptime(date_1, date_format_str)
end =   datetime.strptime(date_2, date_format_str)

# get the difference between two dates as timedelta object
diff = end.date() - start.date()

print('Difference between dates in days:')
print(diff.days)

Output:

Difference between dates in days:
769

Python Example 4: Get difference between two datetimes in days

If you have some existing datetime objects instead of string then we can get the difference between those two datetime objects in days like this,

from datetime import datetime

date_1 = datetime(2021, 7, 2)
date_2 = datetime(2021, 7, 24)

# get the difference between two datetimes as timedelta object
diff = date_2 - date_1

print('Difference between dates in days:')
print(diff.days)

Output:

Difference between dates in days:
22

The different is 22 days.

Python Example 5: Get difference between two dates in days using pandas

Suppose we have two dates in string format. We can convert them to datetime object using pandas.to_datetime() function. Then we will subtract the datetime objects to get the duration in the datetime.timedelta object. Then using the properties of timedelta object, we will fetch the days in between the two dates. For example,

import pandas as pd
from datetime import datetime

date_1 = '2/7/2021'
date_2 = '24/7/2021'


start = pd.to_datetime(date_1, format='%d/%m/%Y')
end = pd.to_datetime(date_2, format='%d/%m/%Y')

# get the difference between two datetimes as timedelta object
diff = end - start

print('Difference between dates in days:')
print(diff.days)

Output:

Difference between dates in days:
22

The different between two dates is 22 days.

Summary:

We learned that, in python how to get the difference between two dates in days.