Add months to a date in Python

avatar

Borislav Hadzhiev

Last updated: Jun 20, 2022

banner

Photo from Unsplash

Add months to a date in Python #

Use the relativedelta class from the dateutil.relativedelta module to add months to a date in Python, e.g. result_1 = date_1 + relativedelta(months=+3). The relativedelta class automatically handles months with different numbers of days.

main.py
from datetime import date, datetime from dateutil.relativedelta import relativedelta # ✅ add months to a date date_1 = date(2023, 6, 24) print(date_1) # 👉️ 2023-06-24 result_1 = date_1 + relativedelta(months=+3) print(result_1) # 👉️ 2023-09-24 # ---------------------------------------- # ✅ add months to current date date_2 = date.today() print(date_2) # 👉️ 2022-06-20 result_2 = date_2 + relativedelta(months=+2) print(result_2) # 👉️ 2022-08-20 # ---------------------------------------- # ✅ add months to date (using datetime object) my_str = '09-24-2023' # 👉️ (mm-dd-yyyy) date_3 = datetime.strptime(my_str, '%m-%d-%Y') print(date_3) # 👉️ 2023-09-24 00:00:00 result_3 = date_3 + relativedelta(months=+2) print(result_3) # 👉️ 2023-11-24 00:00:00 # ---------------------------------------- # ✅ add months to current date (using datetime object) date_4 = datetime.today() print(date_4) # 👉️ 2022-06-20 10:48:05.892375 result_4 = date_4 + relativedelta(months=+3) print(result_4) # 👉️ 2022-09-20 10:48:05.892375

Make sure to import the relativedelta class and the date or datetime classes.

If you don't have the python-dateutil module installed, run the following command:
shell
pip install python-dateutil

The first example shows how to add months to a date object.

main.py
from datetime import date, datetime from dateutil.relativedelta import relativedelta date_1 = date(2023, 6, 24) print(date_1) # 👉️ 2023-06-24 result_1 = date_1 + relativedelta(months=+3) print(result_1) # 👉️ 2023-09-24

Notice that we prefixed the number of months with a plus + to indicate that we want to add the specified number of months.

The relativedelta class automatically handles months with different numbers of days and adding one months never crosses the month boundary.

If necessary, the year will be rolled over automatically (e.g. adding 5 months to a date in November).

The second example adds months to a date object that represents the current date.

main.py
from datetime import date, datetime from dateutil.relativedelta import relativedelta date_2 = date.today() print(date_2) # 👉️ 2022-06-20 result_2 = date_2 + relativedelta(months=+2) print(result_2) # 👉️ 2022-08-20

The date.today method returns a date object that represents the current local date.

The third example creates a datetime object from a date string and adds months to it.

main.py
from datetime import datetime from dateutil.relativedelta import relativedelta my_str = '09-24-2023' # 👉️ (mm-dd-yyyy) date_3 = datetime.strptime(my_str, '%m-%d-%Y') print(date_3) # 👉️ 2023-09-24 00:00:00 result_3 = date_3 + relativedelta(months=+2) print(result_3) # 👉️ 2023-11-24 00:00:00

The datetime.strptime() method returns a datetime object that corresponds to the provided date string, parsed according to the format.

If you have a date string that is formatted in a different way, use this table of the docs to look up the format codes you should pass as the second argument to the strptime() method.

The fourth example adds months to the current date using a datetime object.

main.py
from datetime import datetime from dateutil.relativedelta import relativedelta date_4 = datetime.today() print(date_4) # 👉️ 2022-06-20 10:48:05.892375 result_4 = date_4 + relativedelta(months=+3) print(result_4) # 👉️ 2022-09-20 10:48:05.892375

The datetime.today() method returns the current local datetime.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.