In this artilce we will discuss different ways to add months to 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 add N months to the current date and get the final date as string. Let’s see how to do that,
Add months to 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 add N months to the current date, get the current date as datetime object. Then create a relativedelta object representing the interval of N months and then add that to the current date’s datetime object to get the final date.
Steps to add N months to 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: Add the relativedelta object to the datetime object returned by datetime.today(). It will give us a datetime object pointing to a future date i.e. N months from 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,
Add 20 months to current date in python
Frequently Asked:
- Check If DateTime Is Timezone Aware In Python
- Get current UTC time in Python
- Python: Get difference between two datetimes in minutes
- Convert String to datetime in Python
from datetime import datetime from dateutil.relativedelta import relativedelta # Get local date & time as datetime object current_date = datetime.today() print('Current Date: ', current_date) # Add 20 months to current date n = 20 future_date = current_date + relativedelta(months=n) print('Date - 20 months from current date: ', future_date) print('Date - 20 months from current date: ', future_date.date()) # Convert datetime object to string in required format date_format = '%m/%d/%Y' future_date_str = future_date.strftime(date_format) print('Date as string- 20 months from current date: ', future_date_str)
Output
Current Date: 2021-06-12 18:11:28.849892 Date - 20 months from current date: 2023-02-12 18:11:28.849892 Date - 20 months from current date: 2023-02-12 Date as string- 20 months from current date: 02/12/2023
As today’s date is 2021-06-12 and we added 20 months to it, do the final date became 2023-02-12.
As we added relativedelta (of 20 months duration) to the datetime object containing today’s date, so it returned a new datetime object pointing to the future date i.e. 20 months from now. 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 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 add N months to the current date.
Let’s understand with an example,
Add 10 months to current date in python
from datetime import datetime import pandas as pd # Get local date & time as datetime object current_date = datetime.today() print('Current Date: ', current_date) # Add 10 months to to current date n = 10 future_date = current_date + pd.DateOffset(months=n) print('Date - 10 months from current date: ', future_date) print('Date - 10 months from current date: ', future_date.date()) # Convert datetime object to string in required format date_format = '%m/%d/%Y' future_date_str = future_date.strftime(date_format) print('Date as string- 10 months from current date: ', future_date_str)
Output
Current Date: 2021-06-12 18:12:02.264156 Date - 10 months from current date: 2022-04-12 18:12:02.264156 Date - 10 months from current date: 2022-04-12 Date as string- 10 months from current date: 04/12/2022
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 added this DateOffset object to the datetime object pointing to current date. It returned a datetime object pointing to a future date i.e. after 10 months from the current date.
Summary:
We learned about different ways to add months to current date in python.