Python Add Years to Date Example

Published On: 06/09/2022 | Category: Python


Hi Guys,

In this post, we will learn python 3 add years to date string. let’s discuss about python add years to date in dataframe. We will use how to add year to current date in python. let’s discuss about python add years to date in dataframe. Follow bellow tutorial step of how to add years to date in python.

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

So let's see bellow example:

Example 1: main.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
  
# Get Random Date
myDateString = "2022-08-15"
  
myDate = datetime.strptime(myDateString, "%Y-%m-%d")
  
addYearNumber = 2;
newDate = myDate + relativedelta(years=addYearNumber)
  
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)
Output:
Old Date :
2022-08-15 00:00:00
New Date :
2024-08-15 00:00:00
Example 2: main.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
  
myDate = datetime.today()
    
addYearNumber = 2;
newDate = myDate + relativedelta(years=addYearNumber)
 
print("Old Date :")
print(myDate)
  
print("New Date :")
print(newDate)
Output:
Old Date :
2022-09-06 04:36:47.414489
New Date :
2024-09-06 04:36:47.414489

It will help you....