Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to subtract Python timedelta from date in Python?
The Python datetime module provides various ways for manipulating dates and times. One of its key features is the timedelta object, which represents duration and also the difference between two dates or times.
Subtracting a specific amount of time from a date can be done using timedelta. For example, if we want to find the date a day before today, then we create a timedelta object with days=1 and then subtract it from the current date ?
Subtracting One Day from Today's Date
The basic use case is subtracting a single day from today's date. This can be done by creating a timedelta object representing one day and then subtracting it from the current datetime ?
from datetime import datetime, timedelta
today = datetime.today()
yesterday = today - timedelta(days=1)
print("Today:", today)
print("Yesterday:", yesterday)
The output of the above code is ?
Today: 2024-12-19 16:23:45.123456 Yesterday: 2024-12-18 16:23:45.123456
Subtracting Hours from a Date
We can also subtract a number of hours from a date using timedelta. This method is commonly used when working with time intervals shorter than a day ?
from datetime import datetime, timedelta
now = datetime.now()
five_hours_ago = now - timedelta(hours=5)
print("Now:", now)
print("5 Hours Ago:", five_hours_ago)
The output of the above code is ?
Now: 2024-12-19 16:23:45.123456 5 Hours Ago: 2024-12-19 11:23:45.123456
Subtracting Multiple Units
You can subtract multiple time units simultaneously by combining days, hours, minutes, and seconds in a single timedelta object ?
from datetime import datetime, timedelta
current_time = datetime.now()
past_time = current_time - timedelta(days=2, hours=3, minutes=30, seconds=15)
print("Current Time:", current_time)
print("Past Time:", past_time)
print("Difference:", current_time - past_time)
The output of the above code is ?
Current Time: 2024-12-19 16:23:45.123456 Past Time: 2024-12-17 12:53:30.123456 Difference: 2 days, 3:30:15
Working with Specific Dates
You can subtract timedelta from any specific date, not just the current time. This is useful for calculating dates relative to historical events or future deadlines ?
from datetime import datetime, timedelta
# Specific date
event_date = datetime(2024, 12, 25, 10, 0, 0) # Christmas 2024
one_week_before = event_date - timedelta(weeks=1)
three_days_before = event_date - timedelta(days=3)
print("Event Date:", event_date)
print("One Week Before:", one_week_before)
print("Three Days Before:", three_days_before)
The output of the above code is ?
Event Date: 2024-12-25 10:00:00 One Week Before: 2024-12-18 10:00:00 Three Days Before: 2024-12-22 10:00:00
Common Use Cases
| Time Unit | Example | Use Case |
|---|---|---|
| Days | timedelta(days=7) |
Weekly intervals, date calculations |
| Hours | timedelta(hours=24) |
Scheduling, time zone adjustments |
| Minutes | timedelta(minutes=30) |
Meeting durations, short intervals |
| Seconds | timedelta(seconds=60) |
Precise timing, performance measurement |
Conclusion
The timedelta object makes it easy to subtract time from dates in Python. Use it with days, hours, minutes, or seconds to calculate past dates and times efficiently for various applications.
