Multiplying timedelta instances

An instance of timedelta can be multiplied by an integer value or a floating point value. The Python example calculates the time required to complete different number of laps on a standard distance track.

Timedelta Updated 7 Aug 2021 1 min read

Overview

  • Multiplication of a timedelta instance by an integer or a floating point number is supported through the * operator.

Example

python
# Example Python program that multiplies

            # timdelta instances by integer and floating point

            # values

            from datetime import timedelta

halfLapTime = timedelta(minutes = 3)

# One full lap is two half laps

            oneLapTime  = halfLapTime * 2

print("Time required to complete one lap:")

            print(oneLapTime)

print("Time required to complete two and half laps:")

            print(oneLapTime*2.5)

Output

Output
Time required to complete one lap:

0:06:00

Time required to complete two and half laps:

0:15:00