In this tutorial, I will explain how to subtract a number of days from a date in Python. As a Python developer, I faced this issue while working on a project that required tracking historical data for different cities in the USA. This guide will help you to go through the process step-by-step, with detailed examples and explanations.
datetime Module in Python
Python’s datetime module is a useful tool for manipulating dates and times. Whether you are building a web application, automating reports, or simply need to perform date arithmetic, understanding how to work with dates in Python is essential. This tutorial will cover how to subtract days from a date using the timedelta class from the datetime module.
Read How to Extract Day Number of the Year from a Date in Python?
Import the Required Modules
To work with dates, we need to import the datetime module. Specifically, we will use the datetime and timedelta classes.
from datetime import datetime, timedeltaCheck out How to Validate Passwords in Python?
1. Subtract Days from the Current Date
Let’s start with a simple example: subtracting a specific number of days from the current date. Suppose we want to know the date 10 days ago from today.
# Import the datetime and timedelta classes
from datetime import datetime, timedelta
# Get the current date and time
current_date = datetime.now()
# Define the number of days to subtract
days_to_subtract = 10
# Subtract the days using timedelta
new_date = current_date - timedelta(days=days_to_subtract)
# Print the result
print("Current Date:", current_date)
print("Date 10 Days Ago:", new_date)Output:
Current Date: 2025-01-10 09:27:08.420097
Date 10 Days Ago: 2024-12-31 09:27:08.420097I have executed the above example and added the screenshot below.

In this example, datetime.now() returns the current date and time. The timedelta class is used to represent 10 days. By subtracting this timedelta from the current date, we get the date 10 days ago.
Read Difference Between Class and Instance Variables in Python
2. Subtract Days from a Specific Date
Let’s consider a scenario where you have a specific date and want to subtract days from it. For instance, suppose you have a project deadline on December 1, 2024, and you want to find out the date 15 days before the deadline.
# Import the datetime and timedelta classes
from datetime import datetime, timedelta
# Define the specific date
deadline_date = datetime(2024, 12, 1)
# Define the number of days to subtract
days_to_subtract = 15
# Subtract the days using timedelta
new_date = deadline_date - timedelta(days=days_to_subtract)
# Print the result
print("Deadline Date:", deadline_date)
print("Date 15 Days Before Deadline:", new_date)Output:
Deadline Date: 2024-12-01 00:00:00
Date 15 Days Before Deadline: 2024-11-16 00:00:00I have executed the above example and added the screenshot below.

In this example, we create a datetime object representing December 1, 2024. By subtracting 15 days using timedelta , we determine the date 15 days before the deadline.
Check out How to Validate Email Addresses in Python?
3. Work with Date Strings
Often, dates are provided as strings. Before performing any operations, you need to convert these strings to datetime objects. Let’s say you have a date string representing July 4, 2024, and you want to subtract 20 days from it.
# Import the datetime and timedelta classes
from datetime import datetime, timedelta
# Define the date string
date_string = "2024-07-04"
# Convert the date string to a datetime object
date_object = datetime.strptime(date_string, "%Y-%m-%d")
# Define the number of days to subtract
days_to_subtract = 20
# Subtract the days using timedelta
new_date = date_object - timedelta(days=days_to_subtract)
# Print the result
print("Original Date:", date_object)
print("Date 20 Days Before:", new_date)Output:
Original Date: 2024-07-04 00:00:00
Date 20 Days Before: 2024-06-14 00:00:00I have executed the above example and added the screenshot below.

Here, datetime.strptime() is used to parse the date string into a datetime object. After that, the process is the same as before: subtract the timedelta from the datetime object.
Check out How to Use the round() Function in Python?
Handle Different Date Formats
Date strings can come in various formats. Python’s datetime module provides flexible parsing options to handle different formats. Suppose you have a date string in the format “April 15, 2024”. Here’s how you can handle it:
# Import the datetime and timedelta classes
from datetime import datetime, timedelta
# Define the date string
date_string = "April 15, 2024"
# Convert the date string to a datetime object
date_object = datetime.strptime(date_string, "%B %d, %Y")
# Define the number of days to subtract
days_to_subtract = 30
# Subtract the days using timedelta
new_date = date_object - timedelta(days=days_to_subtract)
# Print the result
print("Original Date:", date_object)
print("Date 30 Days Before:", new_date)In this example, %B %d, %Y is the format string that tells strptime() how to parse the date string.
Read How to Use the Floor() Function in Python?
Example: Analyze Historical Weather Data
Let’s consider a real-world scenario where you need to analyze historical weather data for New York City. Suppose you want to find the weather data for 60 days before a given date.
# Import the datetime and timedelta classes
from datetime import datetime, timedelta
# Define the given date
given_date_string = "2024-11-13"
# Convert the date string to a datetime object
given_date = datetime.strptime(given_date_string, "%Y-%m-%d")
# Define the number of days to subtract
days_to_subtract = 60
# Subtract the days using timedelta
historical_date = given_date - timedelta(days=days_to_subtract)
# Print the result
print("Given Date:", given_date)
print("Historical Date (60 Days Before):", historical_date)In this example, we start with a given date string, convert it to a datetime object, and subtract 60 days to find the historical date. This approach can be used to fetch and analyze weather data from a database or an API.
Check out How to Use Built-In Functions in Python?
Conclusion
In this tutorial, I have explained how to subtract a number of days from a date in Python. datetime and timedelta modules help to achieve this task efficiently. We saw subtracting days from the current date , subtracting days from a specific date, and working with date strings. We also discussed how to handle different date formats and a real-world example.
You may also like to read:
- How to Use the find() Function in Python?
- How to Use Exponential Functions in Python?
- How to Use Counter Function in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.