Skip to main content

How to add Months to a Date in Python

Accurately manipulating dates and times is a common task in software development.

This guide focuses specifically on how to add months to dates in Python, a task that requires careful handling of varying month lengths and year rollovers. We'll leverage the powerful dateutil library to simplify these calculations and ensure accurate results.

Adding Months to a date Object​

The dateutil.relativedelta class is designed for flexible date and time arithmetic, especially when dealing with months. Here’s how to add a specific number of months to a date object:

from datetime import date
from dateutil.relativedelta import relativedelta

initial_date = date(2024, 6, 24)
new_date = initial_date + relativedelta(months=3)
print(new_date) # Output: 2024-09-24
note

The relativedelta(months=N) syntax ensures that month calculations are handled correctly, considering different month lengths and potential year changes. Always use months=N (where N is the number of months) for clarity. The + operator is overloaded to allow direct addition between date/datetime objects and relativedelta objects.

Adding Months to the Current Date​

To perform this operation relative to today’s date, use date.today() and then apply relativedelta:

from datetime import date
from dateutil.relativedelta import relativedelta

today = date.today()
future_date = today + relativedelta(months=2)
print(future_date) # Output: (Varies based on current date)

Working with datetime Objects​

datetime objects encapsulate both date and time information. relativedelta seamlessly handles month additions with these objects.

1. Extracting the Date Component​

If you need only the date part after modification, use the .date() method:

from datetime import datetime
from dateutil.relativedelta import relativedelta

now = datetime.now()
future_datetime = now + relativedelta(months=3)
future_date = future_datetime.date()
print(future_date) # Output: (Date three months in the future)

2. Formatting datetime Output​

Use f-strings with format specifiers to display dates and times as needed:

from datetime import datetime
from dateutil.relativedelta import relativedelta

now = datetime.now()
future_datetime = now + relativedelta(months=3)
formatted_datetime = f'{future_datetime:%Y-%m-%d %H:%M:%S}'
print(formatted_datetime) # Output: (Formatted datetime, e.g., 2024-01-26 10:30:00)
note

Refer to Python’s strftime() documentation for more detailed format codes to customize your date and time strings.

Parsing Date Strings and Adding Months​

Often you'll start with dates represented as strings. Use datetime.strptime() to parse strings and then perform month calculations:

from datetime import datetime
from dateutil.relativedelta import relativedelta

date_str = '09-24-2023' #Format is MM-DD-YYYY
date_obj = datetime.strptime(date_str, '%m-%d-%Y')
future_date = date_obj + relativedelta(months=2)
print(future_date) # Output: 2023-11-24 00:00:00
note

The format string provided to strptime must match the format of your input date string exactly. Inconsistent formats will lead to errors.

Adding Months Using datetime.today()​

datetime.today() provides both date and time information for the current moment, enabling you to perform month calculations:

from datetime import datetime
from dateutil.relativedelta import relativedelta

now_datetime = datetime.today()
future_datetime = now_datetime + relativedelta(months=5)
print(future_datetime) # Output (Current datetime plus 5 months)

Installation of python-dateutil​

If you don't have python-dateutil installed, use pip:

pip install python-dateutil
#or using pip3
pip3 install python-dateutil