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
I have recently released a video on my YouTube channel, which shows the Python syntax of this article, containing some extra explanations. You can find the video below:
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.
If you accept this notice, your choice will be saved and the page will refresh.
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.
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:
- Add Seconds, Minutes & Hours to datetime Object in Python
- Extract Day, Month & Year Separately from datetime Object
- Calculate Time Difference Between Two datetime Objects in Python
- Python Programming Tutorials
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.
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.
2 Comments. Leave new
Thank you!
Thanks for the response, Hemant! Hope the tutorial was helpful.
Regards,
Matthias