How to Add year(s) to a date in Python
Manipulating dates often involves adding or subtracting years.
This guide focuses on how to add years to dates in Python while accounting for potential issues like leap years, particularly February 29th. We'll use the datetime
module's replace()
method for flexible and reliable date calculations.
Adding Years with replace()
The datetime.replace()
method is a direct way to create a new datetime
object by modifying specific attributes, such as the year.
from datetime import datetime
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
# Preserve calendar day (if Feb 29th doesn't exist, set to 28th)
return start_date.replace(year=start_date.year + years, day=28)
my_str = '09-14-2023'
date_1 = datetime.strptime(my_str, '%m-%d-%Y')
new_date = add_years(date_1, 3)
print(new_date) # Output: 2026-09-14 00:00:00
Explanation:
- The
add_years
function takes a date object and the number of years to add. start_date.replace(year=start_date.year + years)
attempts to create a newdatetime
object with the year incremented by the given number of years.- If the resulting date is invalid, for example, if the new year is not a leap year but the start date is Feb 29th, a
ValueError
is raised. In that case, theexcept
block will set the day to 28th of the same month.
Handling February 29th
Since February has 29 days only during leap years, the above function attempts to handle the case when February 29th in the input date doesn't exist in the resulting year, thus avoiding the ValueError
exception by setting the day to 28th. Another approach is to set the day to March 1st:
from datetime import datetime, date
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
# Preserve calendar day (if Feb 29th doesn't exist, set to March 1st)
return start_date + (
date(start_date.year + years, 1, 1) - date(start_date.year, 1, 1)
)
my_str = '02-29-2024'
date_1 = datetime.strptime(my_str, '%m-%d-%Y')
new_date = add_years(date_1, 3)
print(new_date) # Output: 2027-03-01 00:00:00
In the example above, if you start with 02-29-2024
, and add 3 years, the resulting date will be 2027-03-01
because 2027 is not a leap year.
Adding Years to a Date String
To work with dates represented as strings, parse the string to a datetime
object using datetime.strptime()
and then use the add_years
function:
from datetime import datetime
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
return start_date.replace(year=start_date.year + years, day=28)
my_str = '09-14-2023'
date_1 = datetime.strptime(my_str, '%m-%d-%Y')
new_date = add_years(date_1, 3)
print(new_date) # Output: 2026-09-14 00:00:00
Ensure the format string provided to strptime()
matches the format of your date string. Incorrect format specifiers will cause errors.
Adding Years to the Current Date
To add years to the current date, obtain the current date and time using datetime.today()
and then pass it to add_years
function:
from datetime import datetime
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
return start_date.replace(year=start_date.year + years, day=28)
current_date = datetime.today()
future_date = add_years(current_date, 2)
print(future_date) # Output (Varies depending on the current date)
Extracting Date Components After Adding Years
After adding years, you can extract just the date part using the .date()
method:
from datetime import datetime
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
return start_date + (
date(start_date.year + years, 1, 1) - date(start_date.year, 1, 1)
)
now = datetime.now()
future_datetime = add_years(now, 1)
future_date = future_datetime.date()
print(future_date) # Output (Date one year in the future)
Formatting Dates After Adding Years
Use f-strings with format specifiers to display dates as needed:
from datetime import datetime
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
return start_date + (
date(start_date.year + years, 1, 1) - date(start_date.year, 1, 1)
)
now = datetime.now()
future_datetime = add_years(now, 1)
formatted_date = f'{future_datetime:%Y-%m-%d %H:%M:%S}'
print(formatted_date) # Output (Date one year in future, formatted as YYYY-MM-DD HH:MM:SS)
Adding Years Using the date
Class
The same add_years
function works with date
objects as well. This function only handles the date part without the time.
from datetime import date
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
return start_date.replace(year=start_date.year + years, day=28)
date_obj = date(2023, 9, 7)
future_date = add_years(date_obj, 5)
print(future_date) # Output: 2028-09-07
Adding Years to Current Date using date.today()
You can use the date.today()
method with the date
class to get the current date and use the add_years
function:
from datetime import date
def add_years(start_date, years):
try:
return start_date.replace(year=start_date.year + years)
except ValueError:
return start_date.replace(year=start_date.year + years, day=28)
current_date = date.today()
future_date = add_years(current_date, 6)
print(future_date)