How to Add Years to Date in Python?

By Hardik Savani October 30, 2023 Category : Python

If you need to see example of python add years to date example. let’s discuss about how to add years to date in python. if you have question about how to add year to current date in python then I will give simple example with solution. it's simple example of python add years to date in dataframe.

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.

You can use this examples with python3 (Python 3) version.

Example 1: Python Add Years to Date

main.py

from datetime import datetime
from dateutil.relativedelta import relativedelta
  
# Get Random Date
myDateString = "2022-06-01"
  
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-06-01 00:00:00
New Date :
2024-06-01 00:00:00

Example 2: Python Add Years to Current Date

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-06-15 09:30:01.236313
New Date :
2024-06-15 09:30:01.236313

I hope it can help you...

Tags :
Shares