Python timedelta

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 Python timedelta to predict the past dates and future dates.

In this section, we show the maximum number of examples of this Python timedelta. The syntax of this Python datetime timedelta function is

datetime.timedelta(days = 0, seconds = 0, microseconds = 0, milliseconds = 0, minutes = 0, hours = 0, weeks = 0)

Python timedelta Examples

It is a basic example of the datetime timedelta.

# Python timedelta Example
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))

timedelta function in datetime module output

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, resolution to return the most negative, most positive, and the smallest difference between two non-equal timedelta objects.

import datetime
 
print("Minimum = ", datetime.timedelta.min)
print("Maximum = ", datetime.timedelta.max)
print("Resoultion = ", datetime.timedelta.resolution)

timedelta min, max and resolution output

Minimum = -999999999 days, 0:00:00
Maximum = 999999999 days, 23:59:59.999999
Resoultion = 0:00:00.000001

Python timedelta Future Dates

Let me give you a simple timedelta example to return the dates one year from now and two years from now. We can say something like predicting future dates using this timedelta in datetime. 

# Python timedelta Example
 
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 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 timedelta function to find the past dates. From the below timedelta code, days = 730 means subtracting 730 days from the current date.

from datetime import datetime, timedelta
 
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 using the timedelta function.

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 thetimedelta function to find the date before and after 2 weeks / 4 weeks from now. 

from datetime import datetime, 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))

timedelta 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 timedelta function to return the date before and after 2 days from now. 

from datetime import datetime, timedelta
 
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))

timedelta 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. 

from datetime import datetime, timedelta
 
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 using the timedelta function

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

Python timedelta Hours

This timedelta of hours returns the date and time before and after two hours from now.

from datetime import datetime, timedelta
 
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))

timedelta 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

Using the Python timedelta to return DateTime before two minutes, and after 6 minutes from now.

from datetime import datetime, timedelta
 
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))

timedelta 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

timedelta Seconds

In this example, we use timedelta to return DateTime before twenty seconds, and after 90 seconds from now.

from datetime import datetime, timedelta
 
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))

timedelta 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

It prints date and time before 10000 milliseconds and after 9000000 milliseconds from now.

from datetime import datetime, timedelta
 
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))

timedelta 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 timedelta example, we use more than one argument (multiple arguments). current_dt – timedelta(days = 2, hours = 2) returns date and time before 2 days and 2 hours from now.

from datetime import datetime, timedelta
 
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))
Python timedelta 12

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. current_dt – timedelta(2, 2) returns date and time before 2 days and 2 seconds from now.

from datetime import datetime, timedelta
 
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))

timedelta 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 the timedelta with argument values to predict future dates.

from datetime import datetime, timedelta
 
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))

timedelta function with Multiple argument output

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 the timedelta arguments. For instance, current_dt + timedelta(days = -35) returns date before 35 days from now.

from datetime import datetime, timedelta
 
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))

Using Negative Values in timedelta function

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 using timedelta

This timedelta example prints the total number of seconds in an Hour, Day, Year, and Week

from datetime import datetime, timedelta
 
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())

timedelta function to find 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 the timedelta function to predict future dates. Next, we use the strftime function to format the date and times.

from datetime import datetime, timedelta
 
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 using timedelta 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 timedelta.

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