How to Add year(s) to a date in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# Add year(s) to a date in Python

Use the datetime.replace() method to add years to a date.

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: # ๐Ÿ‘‡๏ธ 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 # ----------------------------------------------- # โœ… Add years to the current date current_date = datetime.today() print(current_date) # ๐Ÿ‘‰๏ธ 2023-02-18 18:57:28.484966 result_2 = add_years(current_date, 2) print(result_2) # ๐Ÿ‘‰๏ธ 2025-02-18 18:57:28.484966

add years to date

The code for this article is available on GitHub

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 code for this article is available on GitHub

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 for the second argument to the strptime() method.

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

# The current date might be February 29th

The current date might be 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)
The code for this article is available on GitHub

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 current date might be february 29

# Adding years to the current date

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) # ๐Ÿ‘‰๏ธ 2023-07-22 20:24:47.538361 result_2 = add_years(current_date, 2) print(result_2) # ๐Ÿ‘‰๏ธ 2025-07-22 20:24:47.538361
The code for this article is available on GitHub

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

# Only extract the date components

If you only need to extract the date after the operation, call the date() method on the datetime object.

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) ) now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-02-18 18:59:48.402212 result = add_years(now, 1) print(result) # ๐Ÿ‘‰๏ธ 2024-02-18 18:59:48.402212 # ๐Ÿ‘‡๏ธ Only get a date object print(result.date()) # ๐Ÿ‘‰๏ธ 2024-02-18
The code for this article is available on GitHub

# Formatting the date

If you need to format the date in a certain way, use a formatted string literal.

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 + ( date(start_date.year + years, 1, 1) - date(start_date.year, 1, 1) ) now = datetime.now() print(now) # ๐Ÿ‘‰๏ธ 2023-02-18 19:00:11.870596 result = add_years(now, 1) print(result) # ๐Ÿ‘‰๏ธ 2024-02-18 19:00:11.870596 # ๐Ÿ‘‡๏ธ Format the date print(f'{result:%Y-%m-%d %H:%M:%S}') # ๐Ÿ‘‰๏ธ 2024-02-18 19:00:11

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format specification mini-language in expression blocks.

# Add years to a date using the date() class

You can also use 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

Here is an example that 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 the 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 code for this article is available on GitHub

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev