The Python timedelta function is available in the datetime library. We use this Python timedelta function to calculate the difference between two dates. Or, you can use the timedelta to predict the past dates and future dates.
In this section, we show the maximum number of examples of this Python datetime timedelta object, and its syntax is
datetime.timedelta(days = 0, seconds = 0, microseconds = 0, milliseconds = 0, minutes = 0, hours = 0, weeks = 0)
Python timedelta Example
It is a basic example of the datetime timedelta.
import datetime print("Microseconds = ", datetime.timedelta(microseconds = 1)) print("Milliseconds = ", datetime.timedelta(milliseconds = 1)) print("Seconds = ", datetime.timedelta(seconds = 1)) print("Minutes = ", datetime.timedelta(minutes = 1)) print("Hours = ", datetime.timedelta(hours = 1)) print("Days = ", datetime.timedelta(days = 1)) print("Weeks = ", datetime.timedelta(weeks = 1))
Microseconds = 0:00:00.000001
Milliseconds = 0:00:00.001000
Seconds = 0:00:01
Minutes = 0:01:00
Hours = 1:00:00
Days = 1 day, 0:00:00
Weeks = 7 days, 0:00:00
Python timedelta attributes
The datetime timedelta object has three special attributes min, max, and resolution. And they return the most negative, most positive, and the smallest difference between two non-equal objects.
import datetime print("Minimum = ", datetime.timedelta.min) print("Maximum = ", datetime.timedelta.max) print("Resoultion = ", datetime.timedelta.resolution)
min, max, and resolution output that returns the total number of days.
Minimum = -999999999 days, 0:00:00
Maximum = 999999999 days, 23:59:59.999999
Resoultion = 0:00:00.000001
Python timedelta Future Dates example
Let me give you a simple example to return the dates one year from now and two years from now. We can say something like predicting future dates using this import timedelta class in the datetime object.
Use the first line in all the below examples.
from datetime import datetime, timedelta current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_after_one_year = current_dt + timedelta(days = 365) print("Date and Time After One Year : ", str(dt_after_one_year)) dt_after_two_years = current_dt + timedelta(days = 730) print("Date and Time of Two Years from Now : ", str(dt_after_two_years))
Print Future dates using the Python timedelta function.
Current Date and Time : 2021-05-02 16:40:59.308324
Date and Time After One Year : 2022-05-02 16:40:59.308324
Date and Time of Two Years from Now : 2023-05-02 16:40:59.308324
Python timedelta Past Dates
Here, we subtract days within the function to find the past dates. From the below code, days = 730 means subtracting 730 days from the current date.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_two_years = current_dt - timedelta(days = 730) print("Date and Time before Two Years from Now : ", str(dt_before_two_years)) dt_before_six_years = current_dt - timedelta(days = 2190) print("Date and Time before Six Years from Now : ", str(dt_before_six_years)) dt_before_thirty_years = current_dt - timedelta(days = 10950) print("Date and Time before Thirty Years : ", str(dt_before_thirty_years))
Print Past dates
Current Date and Time : 2021-05-02 16:41:41.735466
Date and Time before Two Years from Now : 2019-05-03 16:41:41.735466
Date and Time before Six Years from Now : 2015-05-04 16:41:41.735466
Date and Time before Thirty Years : 1991-05-10 16:41:41.735466
Python timedelta Week
We are using the weeks argument inside the function to find the date before and after 2 weeks / 4 weeks from now. First, from datetime import timedelta.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_two_weeks = current_dt - timedelta(weeks = 2) print("DateTime before Two Weeks from Now : ", str(dt_before_two_weeks)) dt_after_two_weeks = current_dt + timedelta(weeks = 2) print("DateTime after Two Weeks from Now : ", str(dt_after_two_weeks)) print() dt_before_four_weeks = current_dt - timedelta(weeks = 4) print("DateTime before Four Weeks from Now : ", str(dt_before_four_weeks)) dt_after_six_weeks = current_dt + timedelta(weeks = 6) print("DateTime after Six Weeks from Now : ", str(dt_after_six_weeks))
Week output
Current Date and Time : 2021-05-02 16:42:36.626051
DateTime before Two Weeks from Now : 2021-04-18 16:42:36.626051
DateTime after Two Weeks from Now : 2021-05-16 16:42:36.626051
DateTime before Four Weeks from Now : 2021-04-04 16:42:36.626051
DateTime after Six Weeks from Now : 2021-06-13 16:42:36.626051
Python timedelta Days
We used the days argument inside the timdelta function to return the date before and after 2 days from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_two_days = current_dt - timedelta(days = 2) print("Date and Time before Two Days from Now : ", str(dt_before_two_days)) dt_after_two_days = current_dt + timedelta(days = 2) print("Date and Time of Two Days from Now : ", str(dt_after_two_days))
days output
Current Date and Time : 2021-05-02 18:18:45.436689
Date and Time before Two Days from Now : 2021-04-30 18:18:45.436689
Date and Time of Two Days from Now : 2021-05-04 18:18:45.436689
Here, Python returns the date (not Date and Time) before and after two days from now.
dt = datetime.now() current_dt = dt.date() print("Current Date and Time : ", str(current_dt)) dt_before_two_days = current_dt - timedelta(days = 2) print("Date and Time before Two Days from Now : ", str(dt_before_two_days)) dt_after_five_days = current_dt + timedelta(days = 2) print("Date and Time of Five Days from Now : ", str(dt_after_five_days))
Get Past and Future Dates
Current Date and Time : 2021-05-02
Date and Time before Two Days from Now : 2021-04-30
Date and Time of Five Days from Now : 2021-05-04
timedelta hours
This hour code returns the date and time before and after two hours from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_two_hours = current_dt - timedelta(hours = 2) print("Date and Time before Two Hours from Now : ", str(dt_before_two_hours)) dt_after_two_hours = current_dt + timedelta(hours = 2) print("Date and Time of Two Hours from Now : ", str(dt_after_two_hours))
hours output
Current Date and Time : 2021-05-02 18:32:28.658184
Date and Time before Two Hours from Now : 2021-05-02 16:32:28.658184
Date and Time of Two Hours from Now : 2021-05-02 20:32:28.658184
timedelta minutes
Use this to return DateTime before two minutes and after 6 minutes from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_two_minutes = current_dt - timedelta(minutes = 2) print("Date and Time before Two Minutes from Now : ", str(dt_before_two_minutes)) dt_after_six_minutes = current_dt + timedelta(minutes = 6) print("Date and Time After Six Minutes from Now : ", str(dt_after_six_minutes))
Minutes output
Current Date and Time : 2021-05-02 18:31:35.962983
Date and Time before Two Minutes from Now : 2021-05-02 18:29:35.962983
Date and Time After Six Minutes from Now : 2021-05-02 18:37:35.962983
Python timedelta Seconds
In this example, we use the timedelta function to return DateTime before twenty seconds and after 90 seconds from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_twenty_seconds = current_dt - timedelta(seconds = 20) print("DateTime before Twenty seconds : ", str(dt_before_twenty_seconds)) dt_after_ninty_seconds = current_dt + timedelta(seconds = 90) print("DateTime After Ninty seconds : ", str(dt_after_ninty_seconds))
seconds output
Current Date and Time : 2021-05-02 16:39:06.479500
DateTime before Twenty seconds : 2021-05-02 16:38:46.479500
DateTime After Ninty seconds : 2021-05-02 16:40:36.479500
timedelta milliseconds
The timedelta function prints the date and time before 10000 milliseconds and after 9000000 milliseconds from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_mseconds = current_dt - timedelta(milliseconds = 10000) print("DateTime before Ten Thousand Milliseconds : ", str(dt_before_mseconds)) dt_after_mseconds = current_dt + timedelta(milliseconds = 900000) print("DateTime After 900000 Milliseconds : ", str(dt_after_mseconds))
Milliseconds output
Current Date and Time : 2021-05-02 16:38:29.428497
DateTime before Ten Thousand Milliseconds : 2021-05-02 16:38:19.428497
DateTime After 900000 Milliseconds : 2021-05-02 16:53:29.428497
Python timedelta Multiple Arguments
In this example, we use more than one argument (multiple arguments). current_dt – timedelta(days = 2, hours = 2) returns the date and time before 2 days and 2 hours from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_days = current_dt - timedelta(days = 2, hours = 2) print("Before Date and Time : ", str(dt_before_days)) dt_after_days = current_dt + timedelta(days = 3, hours = 4) print("After Date and Time : ", str(dt_after_days)) dt_before_days = current_dt - timedelta(days = 2, hours = 2, minutes = 10) print("Before Date and Time : ", str(dt_before_days)) dt_after_days = current_dt + timedelta(days = 3, hours = 4, seconds = 20) print("After Date and Time : ", str(dt_after_days))
If you know the order of the arguments and you are using them in the same order, then you don’t need the name. I mean, you can place argument values. Here, current_dt – timedelta(2, 2) returns the date and time before 2 days and 2 seconds from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_days = current_dt - timedelta(2, 2) print("Before Date and Time : ", str(dt_before_days)) dt_before_days = current_dt - timedelta(2, 2, 2) print("Before Date and Time : ", str(dt_before_days)) dt_before_days = current_dt - timedelta(2, 2, 0, 2, 0, 2) print("Before Date and Time : ", str(dt_before_days)) dt_before_days = current_dt - timedelta(2, 2, 0, 2, 0, 2, 1) print("Before Date and Time : ", str(dt_before_days))
with arguments output
Current Date and Time : 2021-05-02 16:36:23.452245
Before Date and Time : 2021-04-30 16:36:21.452245
Before Date and Time : 2021-04-30 16:36:21.452243
Before Date and Time : 2021-04-30 14:36:21.450245
Before Date and Time : 2021-04-23 14:36:21.450245
Here, we are using timedelta with argument values to predict future dates.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_after_days = current_dt + timedelta(3, 3) print("After Date and Time : ", str(dt_after_days)) dt_after_days = current_dt + timedelta(3, 3, 3, 3) print("After Date and Time : ", str(dt_after_days)) dt_after_days = current_dt + timedelta(3, 3, 0, 0, 3, 3) print("After Date and Time : ", str(dt_after_days)) dt_after_days = current_dt + timedelta(3, 0, 0, 4, 2, 3, 1) print("After Date and Time : ", str(dt_after_days)) dt_after_days = current_dt + timedelta(3, 0, 0, 0, 0, 0, 5) print("After Date and Time : ", str(dt_after_days))
Current Date and Time : 2021-05-02 16:35:20.057643
After Date and Time : 2021-05-05 16:35:23.057643
After Date and Time : 2021-05-05 16:35:23.060646
After Date and Time : 2021-05-05 19:38:23.057643
After Date and Time : 2021-05-12 19:37:20.061643
After Date and Time : 2021-06-09 16:35:20.057643
Python timedelta Negative Values
Until now, we are subtracted the current date and time with timedelta values. However, you can use negative values as arguments. For instance, current_dt + (days = -35) returns a date before 35 days from now.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_after_days = current_dt + timedelta(days = -35) print("After Date and Time : ", str(dt_after_days)) dt_after_days = current_dt + timedelta(days = -3, hours = -4) print("After Date and Time : ", str(dt_after_days)) dt_after_days = current_dt + timedelta(days = -10, hours = -40, minutes = -10) print("After Date and Time : ", str(dt_after_days))
Current Date and Time : 2021-05-02 16:34:19.936194
After Date and Time : 2021-03-28 16:34:19.936194
After Date and Time : 2021-04-29 12:34:19.936194
After Date and Time : 2021-04-21 00:24:19.936194
Find total Seconds
This timedelta example prints the total number of seconds in an Hour, Day, Year, and Week
dt = timedelta(hours = 1) print("One Hour = ", dt) print("Total Seconds in One Hour = ", dt.total_seconds()) dt = timedelta(days = 1) print("One Day = ", dt) print("Total Seconds in One day = ", dt.total_seconds()) dt = timedelta(days = 365) print("Total Days = ", dt) print("Total Seconds in a Year = ", dt.total_seconds()) dt = timedelta(weeks = 1) print("One Week = ", dt) print("Total Seconds in a Week = ", dt.total_seconds())
One Hour = 1:00:00
Total Seconds in One Hour = 3600.0
One Day = 1 day, 0:00:00
Total Seconds in One day = 86400.0
Total Days = 365 days, 0:00:00
Total Seconds in a Year = 31536000.0
One Week = 7 days, 0:00:00
Total Seconds in a Week = 604800.0
Python timedelta strftime
Using this timedelta function to predict future dates. Next, we use the strftime function to format the date and times.
current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_after = current_dt + timedelta(days = 30) print("Formatted DateTime : ", dt_after.strftime('%Y/%M/%d %H:%M:%S %p')) dt_after = current_dt + timedelta(days = 365) print("Formatted DateTime : ", dt_after.strftime('%A, %d %b %Y')) dt_after = current_dt + timedelta(days = 365) print("Formatted DateTime : ", dt_after.strftime('%A, %d %b %Y %H:%M:%S %p'))
Predicting Future Dates output
Current Date and Time : 2021-05-02 16:32:18.348961
Formatted DateTime : 2021/32/01 16:32:18 PM
Formatted DateTime : Monday, 02 May 2022
Formatted DateTime : Monday, 02 May 2022 16:32:18 PM
Python Time difference
Finding the time difference between the current date and the date returned by the class in the datetime module.
from datetime import datetime, timedelta current_dt = datetime.now() print("Current Date and Time : ", str(current_dt)) dt_before_ten_days = current_dt - timedelta(days = 10) print("DateTime before Ten Days from Now : ", str(dt_before_ten_days)) print("Time Difference = ", current_dt - dt_before_ten_days) dt_after_two_days = current_dt + timedelta(days = 2) print("DateTime of Two Days from Now : ", str(dt_after_two_days)) print("Time Difference = ", current_dt - dt_after_two_days)
Time difference output.
Current Date and Time : 2021-05-02 16:30:49.956443
DateTime before Ten Days from Now : 2021-04-22 16:30:49.956443
Time Difference = 10 days, 0:00:00
DateTime of Two Days from Now : 2021-05-04 16:30:49.956443
Time Difference = -2 days, 0:00:00