Last updated: Apr 8, 2024
Reading timeยท4 min
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.
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
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.
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.
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 for the second argument to the
strptime()
method.
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.
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.
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.
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
datetime.today()
method returns the current local datetime
.
If you only need to extract the date after the operation, call the date()
method on the datetime
object.
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
If you need to format the date in a certain way, use a formatted string literal.
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.
date()
classYou can also use the date()
class instead of the datetime
class when adding
years to a date.
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.
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
date.today()
method returns a date
object that represents the current local date.
You can learn more about the related topics by checking out the following tutorials: