Open In App

How to add and subtract days using DateTime in Python?

Last Updated : 03 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python’s built-in datetime module provides powerful tools to work with dates and times. One of its common tasks is manipulating dates by adding or subtracting days, hours, minutes and more. It's key classes in the datetime module.

Name

Description

dateIt shows the date according to the Georgian calendar with attributes are year, month and day.
timeIt shows time, independent of any particular day with attributes are hour, minute, second, microsecond and tzinfo.
DateTimeIt is a collection of Dates and Times with the attributes year, month, day, hour, minute, second, microsecond and tzinfo.
timedeltaIt is used to manipulate Date.
tzinfoIt provides information about the Time zone.

Adding and Subtracting Days with timedelta

To add or subtract days, you use the timedelta class, which represents a duration. By adding or subtracting a timedelta object to a datetime object, you shift the date and/or time by the specified amount.

Example 1: In this example, 5 days are added and 5 hours are subtracted from the current date and time.

Python
from datetime import datetime, timedelta
print("Current time: ", datetime.now())
print(datetime.now() + timedelta(days=5, hours=-5))

Output

Current time:  2022-07-13 10:02:04.394152
2022-07-18 05:02:04.394152

Explanation: datetime.now() gets the current date and time. timedelta(days=5, hours=-5) adds 5 days and subtracts 5 hours. Combined, they give a datetime 5 days ahead and 5 hours earlier than now.

Example 2. In this example, 5 days are subtracted and 5 hours are added to the current date and time.

Python
from datetime import datetime, timedelta
print("Current time: ", datetime.now())
print(datetime.now() - timedelta(days=5, hours=-5))

Output
Current time:  2025-06-03 06:23:23.178133
2025-05-29 11:23:23.178169

Explanation: datetime.now() gets the current date and time. timedelta(days=5, hours=-5) adds 5 days and subtracts 5 hours. Subtracting it gives a datetime 5 days earlier and 5 hours ahead of now.

Example 3: In this example, we have created a self-defined date-time and subtracted 4 months from the current time to get the date-time of the last 4 months ago.

Python
import datetime      
from dateutil.relativedelta import relativedelta   

a = datetime.datetime(2022, 8, 13, 10, 53, 10)
print("Date time: ", a)  

b = a - relativedelta(months=4) 
print("4 months ago: ", b)

Output
Current Date time:  2022-08-13 10:53:10
4 months ago:  2022-04-13 10:53:10

Explanation: my_datetime is set to a specific date and time. Using relativedelta(months=4) subtracts 4 months from that date. The result is a new datetime exactly 4 months earlier than my_datetime.

Example 4: In this example, we create a custom datetime and add 5 years using relativedelta.

Python
import datetime
from dateutil.relativedelta import relativedelta

a = datetime.datetime(2022, 8, 13, 10, 53, 10)
print("Date time: ", a)

b = a + relativedelta(years=5)
print("After 5 Years: ", b)

Output
Current Date time:  2022-08-13 10:53:10
After 5 Years:  2027-08-13 10:53:10

Explanation: a stores a specific datetime. Adding relativedelta(years=5) to a advances the date by 5 years, resulting in a new datetime exactly 5 years later.


Next Article
Practice Tags :

Similar Reads