Add Days, Months & Years to datetime Object in Python (3 Examples)

 

On this site, I’ll show you how to add days, months, and years to the datetime object in the Python programming language.

The table of content is structured as follows:

Let’s do this!

 

Import datetime Module & Create Starting Date

For the examples of this tutorial, we first need to import the datetime module:

import datetime

Furthermore, we have to create an example datetime object as basis:

my_date = datetime.datetime(2024, 11, 6, 14, 36, 56) 
print(my_date)
# 2024-11-06 14:36:56

Our example date and time is 2024-11-06 14:36:56.

 

Add Days to datetime Object

If we want to add days to a datetime object, we can use the timedelta() function of the datetime module.

In this specific example, we are adding 18 days to our example datetime object:

my_date_days = my_date + datetime.timedelta(days = 18)
print(my_date_days)
# 2024-11-24 14:36:56

The previous Python syntax has created a new data object containing the datetime 2024-11-24 14:36:56, i.e. 18 days later than our input date.

 

Add Months to datetime Object

In case we want to add months to our datetime object, we first have to import the relativedelta() function from the dateutil module:

from dateutil.relativedelta import relativedelta

Next, we can apply the relativedelta() command to add a certain number of months to our date:

my_date_months = my_date + relativedelta(months = 25)
print(my_date_months)
# 2026-12-06 14:36:56

The previous output shows a date 25 months later than our input date.

 

Add Years to datetime Object

Similar to Example 2, we can apply the relativedelta() function to add years to a datetime object.

Consider the Python code below:

my_date_years = my_date + relativedelta(years = 10)
print(my_date_years)
# 2034-11-06 14:36:56

As you can see, we have created a new datetime object that is ten years later than our example date that we have created at the beginning of this tutorial.

 

Video, Further Resources & Summary

Do you need more explanations on how to handle and increment days, months, and years of datetime objects? Then you may have a look at the following YouTube video from the YouTube channel of Kyle Monson.

In the video is shown how the dateutil module is used to handle datetime objects in Python:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you may have a look at the other Python and datetime tutorials on this website:

Summary: This article has illustrated how to insert days, months, and years in the Python programming language. In case you have any additional questions, you may leave them in the comment section.

 

Gottumukkala Sravan Kumar Statistician & Programmer

Note: This article was created in collaboration with Gottumukkala Sravan Kumar. Gottumukkala is a data analyst and programmer who helps to create tutorials on topics such as the datetime module in Python. You may find more information about Gottumukkala and his other articles on his profile page.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top