timedelta.max attribute

The maximum quantity of time that can be represented by the timedelta class is provided by the attribute timedelta.max. The Python example prints the current value of timedelta.max as supported by the implementation.

Timedelta Updated 6 Aug 2021 1 min read

Overview

  • The maximum time duration that can be represented using the timedelta class is provided by the attribute timedelta.max.
  • Trying to create a timedelta of greater than the timedelta.max will result in an OverflowError.

Example

python
# Example Python program that prints

            # the maximum representable time duration

            # using the Python class datetime.timedelta

from datetime import timedelta

# Obtain maximum time duration possible through timedelta

            max = timedelta.max

            print("Maximum timedelta:")

            print(max)

            print(type(max))

# Try creating a timedelta bigger than timedelta.max

            print(timedelta(timedelta.max.days + 1))

Output

Output
Maximum timedelta:

999999999 days, 23:59:59.999999

<class 'datetime.timedelta'>

Traceback (most recent call last):

  File "/Valli/PythonProgs/td_ex2.py", line 14, in <module>

    print(timedelta(timedelta.max.days + 1))

OverflowError: days=1000000000; must have magnitude <= 999999999