Add year(s) to a date in Python

avatar

Borislav Hadzhiev

Last updated: Jun 20, 2022

banner

Photo from Unsplash

Add year(s) to a date in Python #

Use the datetime.replace() method to add years to a date, e.g. start_date.replace(year=start_date.year + years). The replace method will return a new date with the same attributes, except for the year, which will be updated according to the provided value.

main.py
from datetime import datetime, date def add_years(start_date, years): try: return start_date.replace(year=start_date.year + years) except ValueError: # 👇️ preseve calendar day (if Feb 29th doesn't exist, set to 28th) return start_date.replace(year=start_date.year + years, day=28) # ✅ add years to a date my_str = '09-14-2023' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2023-09-14 00:00:00 result_1 = add_years(date_1, 3) print(result_1) # 👉️ 2026-09-14 00:00:00 # ----------------------------------------------- # ✅ add years to current date current_date = datetime.today() print(current_date) # 👉️ 2022-06-20 14:45:55.418010 result_2 = add_years(current_date, 2) print(result_2) # 👉️ 2024-06-20 14:45:55.418010 # ----------------------------------------------- # ✅ using date instead of datetime date_3 = date(2023, 9, 7) print(date_3) # 👉️ 2023-09-07 result_3 = add_years(date_3, 5) print(result_3) # 👉️ 2028-09-07 # ----------------------------------------------- # ✅ add years to current date (using date instead of datetime) date_4 = date.today() print(date_4) # 👉️ 2022-06-20 result_4 = add_years(date_4, 6) print(result_4) # 👉️ 2028-06-20

The add_years function takes the date and the number of years we want to add and returns an updated date.

The datetime.replace method returns an object with the same attributes, except for the attributes which were provided by keyword arguments.

In the examples, we return a new date where the month and the day are the same but the year is updated.

The first example uses the datetime.strptime() method to get a datetime object that corresponds to the provided date string, parsed according to the specified format.

Once we have the datetime object, we can use the replace() method to replace the year.

main.py
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 28th) return start_date.replace(year=start_date.year + years, day=28) # ✅ add years to a date my_str = '09-14-2023' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2023-09-14 00:00:00 result_1 = add_years(date_1, 3) print(result_1) # 👉️ 2026-09-14 00:00:00

The date string in the example is formatted as mm-dd-yyyy.

If you have a date string that is formatted in a different way, use this table of the docs to look up the format codes you should pass as the second argument to the strptime() method.

Since we preserve the month and day of the month, we have to be aware that the month February has 29 days during a leap year, and it has 28 days in a non-leap year.

It could happen that the current date is February 29th and adding X years returns a non-leap year where February 29th is not a valid date.

In this scenario, we update the year and set the day of the month to the 28th.

main.py
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)

An alternative approach is to set the date to March 1st if February 29th doesn't exist in that year.

main.py
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) ) # ✅ add years to a date my_str = '02-29-2024' # 👉️ (mm-dd-yyyy) date_1 = datetime.strptime(my_str, '%m-%d-%Y') print(date_1) # 👉️ 2024-02-29 00:00:00 result_1 = add_years(date_1, 3) print(result_1) # 👉️ 2027-03-01 00:00:00

The second example adds years to the current date.

main.py
from datetime import datetime, 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 = datetime.today() print(current_date) # 👉️ 2022-06-20 14:45:55.418010 result_2 = add_years(current_date, 2) print(result_2) # 👉️ 2024-06-20 14:45:55.418010

The datetime.today() method returns the current local datetime.

The third example uses the date() class instead of the datetime class when adding years to a date.

main.py
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_3 = date(2023, 9, 7) print(date_3) # 👉️ 2023-09-07 result_3 = add_years(date_3, 5) print(result_3) # 👉️ 2028-09-07

The fourth example adds years to a date object that represents the current date.

main.py
from datetime import datetime, 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) # ✅ add years to current date (using date instead of datetime) date_4 = date.today() print(date_4) # 👉️ 2022-06-20 result_4 = add_years(date_4, 6) print(result_4) # 👉️ 2028-06-20

The date.today method returns a date object that represents the current local date.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.