Adding or subtracting time given in hours:minutes:seconds [hh:mm:ss] format.
Algorithm 1
Add hours, minutes,seconds saparately then, if number minutes or seconds exceeds 60 or are less then 0, adjust the answer.
Example 1 (Adding) – Let’s add 2:45:10(two hours, fourty five minutes, ten seconds) and 1:10:05
- Hours = 2 + 1 = 3
- Minutes = 45 + 10 = 55
- Seconds = 10 + 5 = 15
Our answer is 3 hours, 55 minutes, 15 seconds
Example 2 (Subtracting) – Let’s subtract the times form Example 1
- Hours = 2 – 1 = 1
- Minutes = 45 – 10 = 35
- Seconds 10 – 5 = 5
The answer is 1 hour, 35 minutes, 5 seconds
Example 3 (with adjusting) – Let’s add 2:30:10 and 1:45:55
- Hours = 2 + 1 = 3
- Minutes = 30 + 45 = 75 (75 minutes is 1 hour and 15 minutes because 75 – 60 = 15)
- Seconds = 10 + 55 = 65 (1 minute and 5 seconds)
Because number of minutes and number of seconds are greater then 60, we have to adjust (by adding 1) the number of hours and minutes, so the answer is 4 hours, 16 minutes and 5 seconds.
Example 4 (subtracting with adjusting time). Let’s subtract time from Example 3
- Hours = 2 – 1 = 1
- Minutes = 30 – 45 = -15 (-1 hour + 45 seconds)
- Seconds = 10 – 55 = – 45 (-1 minute + 15 seconds)
Because number of minutes (and seconds) is less then zero we have to adjust number of hours (and minutes) by subtracting 1. The answer is 0 hours, 44 minutes, 15 seconds.
Another algorithm for adding time is available here Adding Time – Algorithm 2
[…] Adding Time […]