Subtract months from current date in Python

In this artilce, we will discuss different ways to subtract months from the current date in python.

In python we can get the current local date and timestamp as datetime object using the datetime.today(). Now suppose we want to subtract N months from the current date and get the past date as string. Let’s see how to do that,

Subtract months from current 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 subtract N months from the current date, get the current date as datetime object. Then create a relativedelta object representing the interval of N months and then subtract that from the current date’s datetime object to get the past date.

Steps to subtract N months from the current 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: Subtract the relativedelta object from the datetime object returned by datetime.today(). It will give us a datetime object pointing to a past date i.e. N months before the current 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,

Subtract 20 months from current date in python

from datetime import datetime
from dateutil.relativedelta import relativedelta

current_date = datetime.today()
print('Current Date: ', current_date)

# Subtract 20 months from current date
n = 20
past_date = current_date - relativedelta(months=n)

print('Date - 20 months before current date: ', past_date)
print('Date - 20 months before current date: ', past_date.date())

# Convert datetime object to string in required format
date_format = '%m/%d/%Y'
past_date_str = past_date.strftime(date_format)
print('Date (as string) - 20 months before current date: ', past_date_str)

Output

Current Date:  2021-06-12 19:11:34.550567
Date - 20 months before current date:  2019-10-12 19:11:34.550567
Date - 20 months before current date:  2019-10-12
Date (as string) - 20 months before current date:  10/12/2019 

As today’s date is 2021-06-12 and we subtracted 20 months from it, do the final date became 2019-10-12.

As we subtracted relativedelta (of 20 months duration) from the datetime object containing today’s date, so it returned a new datetime object pointing to the past date i.e. 20 months before today. 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().

Subtract months from current 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 subtract N months from the current date.

Let’s understand with an example,

Subtract 10 months from current date in python

from datetime import datetime
import pandas as pd

current_date = datetime.today()
print('Current Date: ', current_date)

# Subtract 10 months from current date
n = 10
past_date = current_date - pd.DateOffset(months=n)

print('Date - 10 months before current date: ', past_date)
print('Date - 10 months before current date: ', past_date.date())

# Convert datetime object to string in required format
date_format = '%m/%d/%Y'
past_date_str = past_date.strftime(date_format)
print('Date (as string) - 10 months before current date: ', past_date_str)

Output

Current Date:  2021-06-12 19:13:27.632267
Date - 10 months before current date:  2020-08-12 19:13:27.632267
Date - 10 months before current date:  2020-08-12
Date (as string) - 10 months before current date:  08/12/2020  

First we create a datetime object containing today’s local date and time. then we created a DateOffset object by passing months as 10. Then we subtracted this DateOffset object from the datetime object pointing to current date. It returned a datetime object pointing to a past date i.e. after 10 months before the current date.

Summary:

We learned about different ways to subtract months from current 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