How To Add Years Date In Python

31-Dec-2021

.

Admin

How To Add Years Date In Python

Hello Friends,

Now let's see example of how to add years from date day in python. I am going to show you python date to add years example. We will learn add years in date using python.

The datetime module is supplies classes for manipulating dates and times. This article will give you example of how to add years to dates in python.

Here I will give an example for how to add years to date in python. I am use datetime and time module to add years date. So let's see the below example:

Let's start following example.

Example : 1


example1.py

# import datetime module

from datetime import datetime

from dateutil.relativedelta import relativedelta

currentTimeDate = datetime.now() + relativedelta(years=2)

currentTime = currentTimeDate.strftime('%Y-%m-%d')

print(currentTime)

Run Example

python example1.py

Output:

2023-12-31

Example : 2

example2.py

# import datetime module

from datetime import timedelta, date

currentTimeDate = date.today() + relativedelta(years=5)

currentTime = currentTimeDate.strftime('%Y-%m-%d')

print(currentTime)

Run Example

python example2.py

Output:

2024-12-18

Example : 3

example3.py

# import pandas module

import pandas as pd

initial_date = "2021-12-18"

req_date = pd.to_datetime(initial_date) + pd.DateOffset(years=3)

req_date = req_date.strftime('%Y-%m-%d')

print(req_date)

Run Example

python example3.py

Output:

2026-12-31

It will help you....

#Python