Add year(s) to the current date in Python

avatar

Borislav Hadzhiev

Last updated: Jun 22, 2022

banner

Photo from Unsplash

Add year(s) to the current date in Python #

Use the datetime.replace() method to add years to the current date, e.g. now.replace(year=now.year + num_of_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 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) now = datetime.now() print(now) # 👉️ 2022-06-22 20:05:34.004143 result = add_years(now, 1) print(result) # 👉️ 2023-06-22 20:05:34.004143 print(add_years(now, 2)) # 👉️ 2024-06-22 20:05:34.004143

We used the datetime.now method to get the current local date and time.

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 example, we return a new date where the month and the day are the same but the year is updated.

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: # 👇️ preseve 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) ) now = datetime.now() print(now) # 👉️ 2022-06-22 20:13:38.411871 result = add_years(now, 1) print(result) # 👉️ 2023-06-22 20:13:38.411871

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) # 👉️ 2022-06-22 20:13:38.411871 result = add_years(now, 1) print(result) # 👉️ 2023-06-22 20:13:38.411871 # 👇️ only get a date object print(result.date()) # 👉️ 2023-06-22

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) # 👉️ 2022-06-22 20:13:38.411871 result = add_years(now, 1) print(result) # 👉️ 2023-06-22 20:13:38.411871 # 👇️ format the date print(f'{result:%Y-%m-%d %H:%M:%S}') # 👉️ 2023-06-22 20:13:38.411871

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.

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.