In this article, you’ll learn how to find the number of years and months between two dates in Python. This is useful for calculating someone’s age, the length of a subscription or loan, or how long an employee has worked at a company.
This article is part of a series on Python Date and Time.
You’ll learn how to:
- Get the years, months, and days between two dates with
relativedelta. - Get the total number of months between two dates.
- Count calendar months with the built-in datetime module, and understand how that differs from full months.
- Calculate a person’s age in years.
- Get an approximate difference as a decimal number of years or months.
Table of contents
Also, see the difference between two dates in days in Python.
How to calculate years and months between two dates
Months and years don’t have a fixed length (a month has 28 to 31 days, and a year has 365 or 366), so you can’t simply divide a number of days. The dateutil package solves this with its relativedelta class, which describes the gap between two dates in years, months, days, and smaller units, following the calendar. The steps below show how to use it.
- Install and import dateutil
dateutil is a third-party package that extends Python’s datetime module. Install it with
pip install python-dateutil(it’s already installed if you use pandas), then import the class withfrom dateutil.relativedelta import relativedelta. - Convert date strings to datetime objects
If your dates are strings, first convert each string to a datetime object with
datetime.strptime(date_str, format). For example, the format%d/%m/%Ymatches14/8/2019. - Create a relativedelta object
Call
relativedelta(end_date, start_date). Put the later date first. The result describes the interval between the two dates. - Read the years, months, and days
Use the
years,months, anddaysattributes of the relativedelta object. - Get the total months
The
monthsattribute only holds the months left over after the full years (0 to 11). To get the total number of months, calculatedelta.years * 12 + delta.months.
Example: Get Years, Months, and Days between two dates
Let’s find the difference between 14 August 2019 and 16 March 2022.
Output:
relativedelta(years=+2, months=+7, days=+2) 2 years, 7 months, 2 days
From 14 August 2019, two full years take you to 14 August 2021. Seven more months take you to 14 March 2022, and two more days reach 16 March 2022.
Example: Get Only Months between two dates
Note: delta.months holds only the leftover months (0 to 11), not the total. To get the total number of months, multiply the years by 12 and add the months.
Output:
Total months between two dates: 31
What happens if the dates are in the wrong order?
If you pass the earlier date first, all values become negative. Use abs() if you only care about the size of the gap:
Output:
-2 -7 -2 Total months: 31
Calculate Age in Years
A person’s age is the number of full years since their birth date, which is exactly what relativedelta(...).years returns:
Output:
27 28
Use date.today() as the second argument to get someone’s current age. Without dateutil, you can compare the month and day yourself:
Output:
27
Difference between two dates in months using datetime module
If you don’t want to install dateutil, you can count calendar months with the built-in datetime module. This only looks at the year and month and ignores the day:
months = (end.year - start.year) * 12 + (end.month - start.month)Code language: Python (python)
Output:
Difference between dates in months: 3
Calendar months vs full months: The formula above counts how many times the month changed, not how many full months passed. For example, from 30 January to 1 February is only 2 days, but the month changed once, so the formula returns 1. relativedelta counts full months and returns 0. Use the formula for questions like “how many monthly invoices were sent?”, and relativedelta when you need the real elapsed time.
Output:
Calendar months: 1 Full months: 0 | days: 2
Calculate months between two datetime objects
If you already have datetime objects, there’s nothing to convert. relativedelta also includes hours, minutes, and seconds in its result:
Output:
relativedelta(months=+3, days=+30, hours=+19, minutes=+10, seconds=+10) Total months: 3
Because the end time (04:25) is earlier in the day than the start time (09:15), only 3 full months have passed (plus 30 days and some hours), not 4. The calendar-month formula would return 4.
Get the Difference as a Decimal Number of Years or Months
Sometimes an approximate value like “2.59 years” is enough, for example in charts or statistics. Divide the number of days by the average length of a year (365.25 days) or a month (about 30.44 days):
Output:
Days: 945 Years (approx.): 2.59 Months (approx.): 31.0
These are estimates. Use relativedelta when you need exact calendar results.
Months Between Dates in pandas
In pandas, convert the dates to monthly periods and subtract them. The result counts calendar months, like the datetime formula above:
Output:
start end months 0 2019-08-14 2022-03-16 31 1 2021-12-24 2022-03-26 3
Summary
| Task | Use |
|---|---|
| Years, months, and days | relativedelta(end, start) |
| Total full months | delta.years * 12 + delta.months |
| Age in years | relativedelta(today, birth_date).years |
| Calendar months (no install) | (end.year - start.year) * 12 + (end.month - start.month) |
| Approximate years | (end - start).days / 365.25 |
