Add months to a date in Python

In this artilce we will discuss different ways to add months to a given date in python.

Suppose we have a date ’21/1/2021′ and we want to add N months to it and N can be 1, 20, 50 or any other number. We want the final date after adding N months in the given date. Let’s see how to do that,

Add months to a date in Python using relativedelta

In python, the dateutil module provides a class relativedelta, which represents an interval of time. The relativedelta class has following attributes which tells about the duration,

  • Year
  • Month
  • Day
  • Hours
  • Minutes
  • Seconds
  • Microseconds

To add N months in a given date, create a relativedelta object representing the interval of N months and then add that to the datetime object to get the final date.

Detailed steps to add N months to date are as follows,

  • Step 1: If the given date is in a string format, then we need to convert it to the datetime object. For that we can use the datetime.strptime() function. Whereas, if the given date is already a datetime object, then you can skip this step.
  • Step 2: Create an object of relativedelta, to represent an interval of N months. For that, pass the argument months with value N in the relativedelta constructor.
  • Step 3: Add the relativedelta object to the datetime object. It will give us a datetime object point to a date i.e. N months after the given date.
  • Step 4: If you want the final date in string format, then convert the datetime object to string using strftime(). You can pass the format string as argument and it will convert the datetime object to a string of the specified format.

Let’s understand with an example,

Add 20 months to a date in python

from datetime import datetime
from dateutil.relativedelta import relativedelta

given_date = '21/1/2021'
print('Give Date: ', given_date)

date_format = '%d/%m/%Y'
dtObj = datetime.strptime(given_date, date_format)

# Add 20 months to a given datetime object
n = 20
future_date = dtObj + relativedelta(months=n)

print('Date after 20 months: ', future_date)
print('Date after 20 months: ', future_date.date())

# Convert datetime object to string in required format
future_date_str = future_date.strftime(date_format)
print('Date after 20 months (as string): ', future_date_str)

Output

Give Date:  21/1/2021
Date after 20 months:  2022-09-21 00:00:00   
Date after 20 months:  2022-09-21
Date after 20 months (as string):  21/09/2022

We added 20 months in the date ’21/1/2021′ to make it ’21/09/2022′.

As we added relativedelta (of 20 months duration) to the datetime object, so it returned a new datetime object pointing to the final date. As datetime object has the the timestamp also, therefore it also got printed. If you want date only, then you can fetch the date object from datetime object using date() function, just like we did in above example. In the end we converted the datetime object to the required string format using datetime.strftime().

Add months to a date in Python using Pandas

Pandas provide a class DateOffset, to store the duration or interval information. It is mostly used to increment or decrement a timestamp. It can be used with datetime module to to add N months to a date.

Let’s understand with an example,

Add 10 months to a date in python

from datetime import datetime
import pandas as pd

given_date = '1/21/2021'
print('Give Date: ', given_date)

# Convert date string to datetime object
date_format = '%m/%d/%Y'
dtObj = datetime.strptime(given_date, date_format)

# Add 10 months to a given datetime object
n = 10
future_date = dtObj + pd.DateOffset(months=n)

print('Date after 10 months: ', future_date)
print('Date after 10 months: ', future_date.date())

# Convert datetime object to string in required format
future_date_str = future_date.strftime(date_format)
print('Date after 10 months (as string): ', future_date_str)

Output

Give Date:  1/21/2021
Date after 10 months:  2021-11-21 00:00:00   
Date after 10 months:  2021-11-21
Date after 10 months (as string):  11/21/2021

We created a DateOffset object by passing months as 10. Then we added this DateOffset object to the datetime object. It returned a datetime object pointing to the another date i.e. after 10 months from the given date.

Summary:

We learned about different ways to add months to a date in python.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top