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.

from datetime import date

today = date.today()

past = date(2020, 9, 2)

#minus the dates
print(today - past)

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

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)

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

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

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

from datetime import timedelta

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

print(delta)

Time delta attributes and methods

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

from datetime import timedelta

print(timedelta.min)
print(timedelta.max)

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

from datetime import timedelta

print(timedelta.resolution)

The total_seconds method

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

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())

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.

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)

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

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)

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.

Multiplication

from datetime import timedelta

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

result = delta * 2

print(result)

Division

from datetime import timedelta

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

result = delta / 2

print(result)

Comparison on time deltas

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

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)

Get absolute value of a timedelta object

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

from datetime import timedelta

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

print(abs(delta))