Subtracting A Time Duration From Another

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:

# 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:

Time remaining:

0:34:13

 


Copyright 2023 © pythontic.com