How To Add Year(s) To The Current Date In Python

Add year(s) to the current date in Python

How to add year(s) to the current date in Python is an interesting topic in Pythonperhaps very useful for those who are new to Python. Here are some suggestions for you. Let’s read this article now.

Add year(s) to the current date in Python

Use the replace() function

Syntax:

str.replace(old, new, count)

Parameters:

  • old: Original string.
  • new: String will replace the original string.
  • count: Specifies the maximum number of character substitutions.

Example:

  • Import datetime module.
  • Initialize the current date.
  • Add years using replace() in conjunction with strftime(), which returns a string representing the date, time, and date values.
import datetime

dateNow = datetime.date.today()
yearsToAdd = dateNow.year + 2
currentDate = dateNow.strftime('%Y-%m-%d')

# Use the replace() function to add years
resultDate = dateNow.replace(year=yearsToAdd).strftime('%Y-%m-%d')

print('Current date:', currentDate)
print('Date after adding:', resultDate)

Output:

Current date: 2022-10-25
Date after adding: 2024-10-25

Use DateOffset method

You also can use DateOffset() method from the pandas module to add years to the current year.

Syntax:

pandas.tseries.offsets.DateOffset()

Example:

  • Import pandas module DateOffset() method of that module.
  • Initialize a datetime string.
  • Then you have to convert the datetime string to a datetime object.
  • Finally, use DateOffset() to add years to the current year.
import pandas as pd
  
# String datetime
currentDate = '2022-10-25'
print('Current date: ' + currentDate)

# Combine the pd.to_datetime function and the pd.DateOffset function to add year to the currentDate
newDate = pd.to_datetime(currentDate) + pd.DateOffset(years = 2)

print('Date after adding : ' + str(newDate))

Output:

Current date: 2022-10-25
Date after adding : 2024-10-25 00:00:00

Use the timedelta64 method

The datetime object is taken from the Python standard library so the object is named datetime64. You can use this to add years to the current year.

Syntax:

numpy.timedelta64

Timedelta64 is just a 64-bit integer that is interpreted in different ways depending on the second parameter you passed in.

Example:

  • Import the numpy module.
  • Create a datetime object using the datetime64() method.
  • Use the timedelta64() method to add years to the current year.
import numpy as np

print('Date current: ' + str(np.datetime64('2022')))

# Use the timedelta64() method to add years to the current year
newDate = np.datetime64('2022') + np.timedelta64(2, 'Y')

print('Date after adding: ' + str(newDate))

Output:

Date current: 2022
Date after adding: 2024

Use relativedelta method

Relative styles are designed to be applied to existing dates and times and can replace specific components of that date or time or represent a period.

Syntax:

relativedelta(datetime1, datetime2)

Parameters:

  • datetime1, datetime2: datetime objects.

Example:

  • Import the datetime module to be able to use the date class.
  • Import the datetime module to be able to use the relativedelta method.
  • Initialize a datetime object using date().
  • Add years with the relativedelta() method.
from datetime import date
from dateutil.relativedelta import relativedelta
  
# The datetime object is created
print('Current date: ' + str(date(2022,10,25)))

# Add years using the relativedelta() method
newDate = date(2022,10,25) + relativedelta(years=2)

print('Date after adding: ' + str(newDate))

Output:

Current date: 2022-10-25
Date after adding: 2024-10-25

Summary

Hopefully, this article will give you more ideas about how to add year(s) to the current date in Python. You should read the article carefully to understand the problem I want to convey more deeply. If you have any questions, please leave a comment, and I will try to answer.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *