add 4 years to a date in python code example
Example: add 3 years to a given date in python code
import datetime
from datetime import date
def addYears(d, years):
try:
#Return same day of the current year
return d.replace(year = d.year + years)
except ValueError:
#If not same day, it will return other, i.e. February 29 to March 1 etc.
return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
print(addYears(datetime.date(2015,1,1), -1))
print(addYears(datetime.date(2015,1,1), 0))
print(addYears(datetime.date(2015,1,1), 2))
print(addYears(datetime.date(2000,2,29),1))