How to add and subtract days using DateTime in Python?
Last Updated :
03 Jun, 2025
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 |
---|
date | It shows the date according to the Georgian calendar with attributes are year, month and day. |
time | It shows time, independent of any particular day with attributes are hour, minute, second, microsecond and tzinfo. |
DateTime | It is a collection of Dates and Times with the attributes year, month, day, hour, minute, second, microsecond and tzinfo. |
timedelta | It is used to manipulate Date. |
tzinfo | It 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))
OutputCurrent 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)
OutputCurrent 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)
OutputCurrent 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.
Similar Reads
How to add Days to a Date in Python? Python provides an in-built module datetime which allows easy manipulation and modification of date and time values. It allows arithmetic operations as well as formatting the output obtained from the DateTime module. The module contains various classes like date, time, timedelta, etc. that simulate
2 min read
How to Add and Subtract Days to and from Date in R? Managing dates and times is a crucial aspect of data analysis, and R provides robust facilities for dealing with date objects. In this post, we will look at how to add and remove days from a date in R. Whether you're dealing with time series data, need to compute future dates, or want to browse thro
4 min read
Date Calculator for adding and subtracting days Using Tkinter - Python Prerequisite: Tkinter, tkcalendar in Tkinter, and DateTime Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkin
3 min read
How to Add or subtract time span to a datetime in R ? The time objects in R can be declared either using POSIXct class, which offers fast manipulation and storage of such objects. External packages in R also help in working with time and dates and allow both comparison and direct arithmetic operations to be performed upon them. In this article, we are
4 min read
How to Add or Subtract Dates in Excel New to Excel and seeking a way to add or subtract dates, then this guide on how to add or subtract dates in Excel will expalin you the right way to manage project timelines, schedules, or due dates. Weâll break down simple steps to add or subtract dates using easy formulas and time-saving tips.How t
5 min read