repr() function on a timedelta instance

The repr() function returns the string representation of a timedelta object in constructor call format. The example prints the constructor call representation for different time durations.

Timedelta Updated 7 Aug 2021 1 min read

Overview

  • For a timedelta instance, the function repr() returns a string containing the constructor invocation which is quantitatively equivalent to the original parameter values.

Example

python
# Example Python program that prints

            # a timedelta instance as a call to the constructor

            from datetime import timedelta

longestTennisMatch = timedelta(hours=11, minutes=5)

            print("Longest tennis match:")

            print(repr(longestTennisMatch))

twentyFourPlus = timedelta(hours=24, minutes=10)

            print("More than 24 hours:")

            print(repr(twentyFourPlus))

Output

Output
Longest tennis match:

datetime.timedelta(seconds=39900)

More than 24 hours:

datetime.timedelta(days=1, seconds=600)