How to add Months to a Date in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Add months to a date in Python

Use the relativedelta class from the dateutil.relativedelta module to add months to a date in Python.

The relativedelta class automatically handles months with different numbers of days.

main.py
from datetime import date, datetime from dateutil.relativedelta import relativedelta date_1 = date(2023, 6, 24) print(date_1) # ๐Ÿ‘‰๏ธ 2023-06-24 # โœ… Add 3 months to a date result = date_1 + relativedelta(months=+3) print(result) # ๐Ÿ‘‰๏ธ 2023-09-24

add months to date

The code for this article is available on GitHub

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

# Installing python-dateutil

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

shell
pip install python-dateutil # ๐Ÿ‘‡๏ธ or with pip3 pip3 install python-dateutil

install python dateutil

The python-dateutil module provides extensions to the standard Python datetime module.

The 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 = date_1 + relativedelta(months=+3) print(result) # ๐Ÿ‘‰๏ธ 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.

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

# Add months to the current date

If you need to add months to the current date, use the date.today() method.

main.py
from datetime import date, datetime from dateutil.relativedelta import relativedelta date_1 = date.today() print(date_1) # ๐Ÿ‘‰๏ธ 2023-07-21 result = date_1 + relativedelta(months=+2) print(result) # ๐Ÿ‘‰๏ธ 2023-09-21

add months to current date

The code for this article is available on GitHub

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

Make sure to import the relativedelta and date classes.

# Extracting the date after adding months

If you only need to extract the date after the operation, call the date() method on the datetime object.

main.py
from datetime import datetime from dateutil.relativedelta import relativedelta now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-07-21 08:39:13.428040 result = now + relativedelta(months=+3) print(result) # ๐Ÿ‘‰๏ธ 2023-10-21 08:39:13.428040 # ๐Ÿ‘‡๏ธ only extract updated date print(result.date()) # ๐Ÿ‘‰๏ธ 2023-10-21

extracting date after adding months

The code for this article is available on GitHub

The datetime.date method returns a date object with the same year, month and day.

# Using a formatted-string literal to format the date

If you need to format the date in a certain way, use a formatted string literal.

main.py
from datetime import datetime from dateutil.relativedelta import relativedelta now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-02-18 18:54:05.944818 result = now + relativedelta(months=+5) print(result) # ๐Ÿ‘‰๏ธ 2023-07-18 18:54:05.944818 # ๐Ÿ‘‡๏ธ only extract updated date print(result.date()) # ๐Ÿ‘‰๏ธ 2023-07-18 print(f'{result:%Y-%m-%d %H:%M:%S}') # ๐Ÿ‘‰๏ธ 2023-07-18 18:54:05

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

# Creating a datetime object from a date string and adding months

You can also create a datetime object from a date string and add months to it.

main.py
from datetime import datetime from dateutil.relativedelta import relativedelta my_str = '09-24-2023' # ๐Ÿ‘‰๏ธ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # ๐Ÿ‘‰๏ธ 2023-09-24 00:00:00 result = date_1 + relativedelta(months=+2) print(result) # ๐Ÿ‘‰๏ธ 2023-11-24 00:00:00

creating datetime object from date string and adding months

The code for this article is available on GitHub

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.

# Adding months to the current date using a datetime object

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

main.py
from datetime import datetime from dateutil.relativedelta import relativedelta date_1 = datetime.today() print(date_1) # ๐Ÿ‘‰๏ธ 2023-07-21 08:44:16.815774 result = date_1 + relativedelta(months=+3) print(result) # ๐Ÿ‘‰๏ธ 2023-10-21 08:44:16.815774

adding months to current date using datetime object

The code for this article is available on GitHub

The datetime.today() method returns the current local date and time.

I've also written an article on how to add days to a date.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev