How to Add Days, Months & Years to a datetime Object in Python (3 Examples)
This post explains how to add days, months, and years to datetime objects in Python.
Creating Example Data & Loading datetime Module
We can load the datetime module as shown below.
import datetime |
import datetime
The data below will be used for the examples of this tutorial.
x = datetime.datetime(2026, 7, 10, 11, 15, 44) print(x) # 2026-07-10 11:15:44 |
x = datetime.datetime(2026, 7, 10, 11, 15, 44) print(x) # 2026-07-10 11:15:44
Example 1: Adding Days to Date & Time Object
The timedelta function can be used to add additional days to a datetime object.
x_days = x + datetime.timedelta(days = 50) print(x_days) # 2026-08-29 11:15:44 |
x_days = x + datetime.timedelta(days = 50) print(x_days) # 2026-08-29 11:15:44
Example 2: Adding Months to Date & Time Object
The Python code below adds months to a datetime object.
from dateutil.relativedelta import relativedelta |
from dateutil.relativedelta import relativedelta
x_months = x + relativedelta(months = 20) print(x_months) # 2028-03-10 11:15:44 |
x_months = x + relativedelta(months = 20) print(x_months) # 2028-03-10 11:15:44
Example 3: Adding Years to Date & Time Object
The relativedelta function can also be applied to add further years to a datetime object in Python.
x_years = x + relativedelta(years = 3) print(x_years) # 2029-07-10 11:15:44 |
x_years = x + relativedelta(years = 3) print(x_years) # 2029-07-10 11:15:44
Further Resources
Please find some related tutorials below.
- Add Hours, Minutes & Seconds to datetime Object in Python (3 Examples)
- Get Year, Month & Day Separately from datetime Object in Python (3 Examples)
- Get Time Difference Between Two Dates & Times in Python (datetime Module)
- All Python Programming Tutorials
Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.