Calculate Number of Years, Months & Days Between Two Dates in Python (2 Examples)


In this tutorial you’ll learn how to calculate the number of years, months, and days between two dates using the Python programming language.

The table of content is structured as follows:

Let’s dive into it!


Example Data & Add-On Libraries

First we have to import date from the datetime module and relativedelta from the dateutil module:

from datetime import date
from dateutil import relativedelta

Then, let’s construct some sample dates to use in our example:

date_1 = date(1991, 10, 20)
date_2 = date(2001, 6, 15)

Date objects have year, month and day fields. They can be used to get more specific information about the date.

print(date_1.year, date_1.month,
print(date_2.year, date_2.month,
# 1991 10 20
# 2001 6 15


Example 1: Calculate the Difference Between Two Dates in Years, Months & Days Format

Note1: We can think of it as date_diff = date_2 – date_1 to remember the parameter order.

Note2: The following if else statement can be used to avoid negative difference results which may occur due to dates being unknown at the start of the program.

if date_1 < date_2:
    date_diff = relativedelta.relativedelta(date_2, date_1)
    date_diff = relativedelta.relativedelta(date_1, date_2)
years = date_diff.years   # 9
months = date_diff.months # 7
days = date_diff.days     # 26
print('{} years {} months {} days'.format(years, months, days))
# 9 years 7 months 26 days

The printed output would be “-9 years -7 months -26 days” if the dates were entered in the wrong order.

Example 2: Calculate Your Lifetime

It’s also possible to calculate your personal lifetime in years, months, and days using Python code. Let’s assume your date of birth is the 18th of May 1999:

today =                    # 2022 11 3
birthday = date(1999, 5, 18)            # 1999 5 18

Next, we can apply the relativedelta function to calculate the lifetime:

date_diff = relativedelta.relativedelta(today, birthday)
years = date_diff.years    # 23
months = date_diff.months  # 5
days = date_diff.days      # 16
print('Congrats! You have been alive for {} years {} months {} days'.format(years, months, days))
# Congrats! You have been alive for 23 years 5 months 16 days


Video, Further Resources & Summary

Do you need more explanations on how to compute time differences using Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain how to return the difference between two dates in years, months, and days using the Python syntax of the present tutorial.


The YouTube video will be added soon.


Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to get the time difference between two dates in years, months, and days. If you have any further questions, you might leave a comment below.


Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.