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 create a timedelta object with the specific number of days and use the + or - operator with a date object to return a new date object representing the result.

Example

In the following program, we find yesterday's and tomorrow's dates relative to today. We use datetime.date.today() 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)

The output of the above code is ?

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 subtract one date object from another, which generates a timedelta object representing the difference in days.

Example

To calculate the difference in days, we subtract today's date from a specified date ?

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.days)
print('Full timedelta  :', days_until_event)

The output of the above code is ?

Days until event: 25
Full timedelta  : 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, we need to use this class. To perform arithmetic, we create datetime.datetime objects and apply timedelta to adjust both date and time components.

Example

Here we add 8 hours and 30 minutes using a timedelta to the current time ?

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)

The output of the above code is ?

Current Time : 2025-06-20 12:00:00
Future Time  : 2025-06-20 20:30:00

Working with Weeks and Months

We can also perform arithmetic with larger time units like weeks. For months and years, we need additional handling since they vary in length ?

import datetime

today = datetime.date.today()

# Adding weeks
next_week = today + datetime.timedelta(weeks=2)
print("Two weeks from today:", next_week)

# Adding approximate months (30 days)
next_month = today + datetime.timedelta(days=30)
print("Approximately 1 month:", next_month)

The output of the above code is ?

Two weeks from today: 2025-07-04
Approximately 1 month: 2025-07-20

Comparison

Operation Method Returns
Add/Subtract Time date + timedelta New date object
Date Difference date1 - date2 timedelta object
Extract Days timedelta.days Integer

Conclusion

Python's datetime module provides powerful tools for date arithmetic using timedelta objects. Use date + timedelta for adding time intervals and date1 - date2 for calculating differences between dates.

Updated on: 2026-03-24T19:27:55+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements