Last updated: Apr 8, 2024
Reading timeยท3 min
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.
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
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:
pip install python-dateutil # ๐๏ธ or with pip3 pip3 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.
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).
If you need to add months to the current date, use the date.today()
method.
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
The
date.today()
method returns a date
object that represents the current local date.
Make sure to import the relativedelta
and date
classes.
If you only need to extract the date after the operation, call the date()
method on the datetime
object.
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
The datetime.date method returns a date object with the same year, month and day.
If you need to format the date in a certain way, use a formatted string literal.
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.
datetime
object from a date string and adding monthsYou can also create a datetime
object from a date string and add months to it.
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
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.
datetime
objectThe following example adds months to the current date using a datetime
object.
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
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.
You can learn more about the related topics by checking out the following tutorials: