The timedelta class  in the datetime module is used to represent a duration i.e the difference between two dates or times. Whenever we perform subtraction operation on date or datetime objects,  the result is a timedelta object containing the difference between the two dates or times.

ExampleEdit & Run
from datetime import date

today = date.today()

past = date(2020, 9, 2)

#minus the dates
print(today - past)
copy
Output:
1601 days, 0:00:00 [Finished in 0.012505182065069675s]

Timedelta objects have three basic attributes i.e days, seconds and microseconds. These attributes specify  the total amount of time  represented by the object.

ExampleEdit & Run
from datetime import datetime

today = datetime.now()

past = datetime(2022, 9, 1)

delta = today - past

print("days: ", delta.days)
print("seconds: ", delta.seconds)
print("microseconds: ", delta.microseconds)
copy
Output:
days:  872 seconds:  3893 microseconds:  115810 [Finished in 0.01164932269603014s]

We can also initialize timedelta objects directly using the following syntax:

Syntax:
timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
copy

All the arguments are optional, and only the days, seconds and microseconds are stored internally in the timedelta object. 

ExampleEdit & Run
from datetime import timedelta

delta = timedelta(
    days=30,
    seconds=27,
    microseconds=10,
    milliseconds=250,
    minutes=5,
    hours=8,
    weeks=2
)

print(delta)
copy
Output:
44 days, 8:05:27.250010 [Finished in 0.011573998257517815s]

Time delta attributes and methods

The min and max class attributes are used to get the smallest and the largest time possible, respectively.

ExampleEdit & Run
from datetime import timedelta

print(timedelta.min)
print(timedelta.max)
copy
Output:
-999999999 days, 0:00:00 999999999 days, 23:59:59.999999 [Finished in 0.011372415348887444s]

The resolution attribute specifies the smallest possible difference between non-equal time-delta objects.

ExampleEdit & Run
from datetime import timedelta

print(timedelta.resolution)
copy
Output:
0:00:00.000001 [Finished in 0.011228971183300018s]

The total_seconds method

This method computes the total number of seconds represented by the timedelta object

ExampleEdit & Run

Get the total seconds on a timedelta

from datetime import datetime

today = datetime.now()

past = datetime(2022, 9, 1)

delta = today - past

print("total seconds: ", delta.total_seconds())
copy
Output:
total seconds:  75344693.164554 [Finished in 0.010792898945510387s]

Operations on timedeltas

We can perform various operations on timedelta object. We can also compare two timedelta objects using the comparison operators like <, > etc.

Addition and Subtraction

The addition operation creates an object representing the sum of two timedelta objects. It is represented by the + operator.

ExampleEdit & Run

add timedeltas

from datetime import timedelta

delta1 = timedelta(days = 1, seconds=20) 
delta2 = timedelta(days = 2, seconds=40)

#add the two timedelta objects 
result = delta2 + delta1 

print(result)
copy
Output:
3 days, 0:01:00 [Finished in 0.011264161206781864s]

The subtraction operation creates an object representing the difference between two datetime.timedelta objects. It is represented by the - operator.

ExampleEdit & Run

subtract timedeltas

from datetime import timedelta

delta1 = timedelta(days = 1, seconds=20) 
delta2 = timedelta(days = 2, seconds=40)

#subtract the two datetime.timedelta objects 
result = delta2 - delta1 

print(result)
copy
Output:
1 day, 0:00:20 [Finished in 0.011376908980309963s]

Multiplication and division

We can perform multiplication and division operations an a timedelta object where the other operand is an integer or a floating point value.

ExampleEdit & Run

Multiplication

from datetime import timedelta

delta = timedelta(days = 1, seconds=20) 

result = delta * 2

print(result)
copy
Output:
2 days, 0:00:40 [Finished in 0.010803384706377983s]
ExampleEdit & Run

Division

from datetime import timedelta

delta = timedelta(days = 1, seconds=20) 

result = delta / 2

print(result)
copy
Output:
12:00:10 [Finished in 0.011320976540446281s]

Comparison on time deltas

We can perform various comparison operations on time deltas using the various comparison operators. 

ExampleEdit & Run
from datetime import timedelta

delta1 = timedelta(days = 1, seconds=20) 
delta2 = timedelta(days = 2, seconds=40)

print(delta1 == delta2)
print(delta1 != delta2)
print(delta1 < delta2)
print(delta2 > delta1)
copy
Output:
False True True True [Finished in 0.01090517919510603s]

Get absolute value of a timedelta object

We can use the builtin  abs() function to get the absolute values of timedelta objects. 

ExampleEdit & Run
from datetime import timedelta

delta = timedelta(days = -1, seconds=20) 

print(abs(delta))
copy
Output:
23:59:40 [Finished in 0.010822857730090618s]