How to Add Months to Date in Python?

By Hardik Savani October 30, 2023 Category : Python

This tutorial will give you example of python add months to date example. This article goes in detailed on how to add months to date in python. I explained simply step by step how to add month to current date in python. This tutorial will give you simple example of python add months to date in dataframe.

In this example, I will give two examples for you of how to add months to date in python and how to add months to today's date in python. therefore, let's see below example code and try it.

Example 1: Python Add Months to Date

main.py

from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDateString = "2022-06-01"
  
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
  
addMonthNumber = 2;
newDate = myDate + relativedelta(months=addMonthNumber)
  
print("Old Date :")
print(myDate) 
  
print("New Date :")
print(newDate)

Output:

Old Date :
2022-06-01 00:00:00
New Date :
2022-08-01 00:00:00

Example 2: Python Add Months to Current Date

main.py

from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDate = datetime.today()
  
addMonthNumber = 2;
newDate = myDate + relativedelta(months=addMonthNumber)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)

Output:

Old Date :
2022-06-13 09:23:30.030986
New Date :
2022-08-13 09:23:30.030986

I hope it can help you...

Tags :
Shares