In this article, we will discuss how to get the difference between two dates in years in python.
In python, the dateutil module provides a class relativedelta, which represents an interval of time. The relativedelta class has following attributes which tells about the duration,
- Year
- Month
- Day
- Hours
- Minutes
- Seconds
- Microseconds
So, to calculate the difference between two dates in years, we can create a relativedelta object to represent the interval between two given dates. Then we can fetch the Year attribute of relativedelta object, it will tell us the difference between two dates in years. Let’s understand with some examples,
Python Example 1: Get difference between two dates in years
If you have some existing datetime objects instead of strings, then we can get the difference between those two datetime objects in years like this,
from datetime import datetime from dateutil import relativedelta date_1 = datetime(2021, 7, 2) date_2 = datetime(2032, 3, 24) # Get the interval between two dates diff = relativedelta.relativedelta(date_2, date_1) print('Difference between dates in years: ', diff.years) print('Complete Difference between two dates: ') print(diff.years , ' years, ', diff.months, ' months and ', diff.days, ' days')
Output:
Difference between dates in years: 10 Complete Difference between two dates: 10 years, 8 months and 22 days
Python Example 2: Get difference between two dates in years
Suppose we have two dates in string format. We can convert them to datetime objects using datetime.strptime() function. Then we will get the interval between two dates as a relativedelta object. Then using the years property of relativedelta object, we will fetch the years in between the two dates. For example,
from datetime import datetime from dateutil import relativedelta 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 interval between two dates diff = relativedelta.relativedelta(end, start) print('Difference between dates in years: ', diff.years) print('Complete Difference between two dates: ') print(diff.years , ' years, ', diff.months, ' months and ', diff.days, ' days')
Output:
Frequently Asked:
Difference between dates in years: 2 Complete Difference between two dates: 2 years, 0 months and 22 days
Python Example 3: Get difference between two timestamps in years
If we have complete timestamps instead of dates only, then we can convert them to datetime objects using datetime.strptime() function and specific format string as argument. Then we will get the interval between two timestamps as a relativedelta object. Then using the years property of relativedelta object, we will fetch the years in between the two timestamps. For example,
from datetime import datetime from dateutil import relativedelta 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 interval between two dates diff = relativedelta.relativedelta(end, start) print('Difference between dates in years: ', diff.years) print('Complete Difference between two dates: ') print(diff.years , ' years, ', diff.months, ' months and ', diff.days, ' days')
Output:
Difference between dates in years: 2 Complete Difference between two dates: 2 years, 1 months and 8 days
Summary:
We learned that, in python how to get the difference between two dates in years.