
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to perform arithmetic operations on a date in Python?
Performing arithmetic operations on dates allows us to calculate differences between dates, add or subtract time intervals, or compare one date with another using the datetime module in Python.
This article will discuss how to perform several arithmetic operations using the datetime module in Python.
Adding and Subtracting Days Using timedelta
In the Python datetime module, timedelta is a class that represents the difference or duration between two dates or times. We can use timedelta objects to perform date arithmetic, such as adding or subtracting a certain number of days, weeks, hours, minutes, etc.
To add or subtract days, we have to create a timedelta object with the specific number of days and by using the + or - operator with a date object, it will return a new date object representing the result.
Example
In the following program, we are going to find yesterday's and tomorrow's dates relative to today. We can use datetime.date.today() method to get the current date, then add or subtract a timedelta of one day.
import datetime today = datetime.date.today() one_day = datetime.timedelta(days=1) yesterday = today - one_day tomorrow = today + one_day print('Today :', today) print('Yesterday :', yesterday) print('Tomorrow :', tomorrow)
Following is the output of the above code:
Today : 2025-06-20 Yesterday : 2025-06-19 Tomorrow : 2025-06-21
Finding the Difference Between Two Dates
To find the difference between two dates, we have to subtract one date object from another; it generates a timedelta object, which represents the difference in days.
Example
To calculate the difference in days, we are going to subtract today's date from the specified date (July 15, 2025).
import datetime today = datetime.date.today() event_date = datetime.date(2025, 7, 15) days_until_event = event_date - today print('Days until event:', days_until_event)
Following is the output of the above code:
Days until event: 25 days, 0:00:00
Using datetime.datetime for Date and Time Arithmetic
The datetime.datetime class represents a combination of a date and time. It stores information including the year, month, day, hour, minute, second, microsecond, and optionally, time zone information.
If we want to manipulate not only dates, but also times, then we need to use this class. To perform arithmetic, we can create datetime.datetime objects and apply timedelta to adjust both date and time components.
Example
Here we are adding 8 hours and 30 minutes (using a timedelta) to the current time (using datetime.datetime.now()).
import datetime now = datetime.datetime.now() delta = datetime.timedelta(hours=8, minutes=30) future_time = now + delta print("Current Time :", now) print("Future Time :", future_time)
Following is the output of the above code:
Current Time : 2025-06-20 12:00:00 Future Time : 2025-06-20 20:30:00