Subtracting a time duration from another

Subtracting one timedelta object from another timedelta object returns the remaining time duration.

Timedelta Updated 6 Aug 2021 1 min read

Overview

  • Subtraction of one timedelta from another returns the remaining time duration. Examples include, time remaining to conclude during an academic examination, time remaining to catch a bus or flight.

Example

python
# Example Python program that subtracts

            # one time duration from another using

            # datetime.timedelta objects

            from datetime import timedelta

totalExamHours  = timedelta(hours = 3)

            timeElapsed     = timedelta(hours = 2, minutes = 25, seconds = 47)

            timeRemaining   = totalExamHours - timeElapsed

            print("Time remaining:")

            print(timeRemaining)

Output

Output
Time remaining:

0:34:13