# subtracting two time values in 24 hour clock format

• ### Question

• I am coding a program which will ask for the start time and end time of a run in 24 hour formst (HH:MM:SS) . I Would like to know how i could subtrsct these 2 variables to leave the total time it took for the runner to complete the race, preferably in hours. Thanks
Sunday, May 31, 2015 2:54 PM

• There are lots of way to performing a calculation like this. The one that usually pops into your head first is treating it like a three digit, base 60 number. I have found that the solution when taking that path is counter-intuitive and always ends up with lots of IF statements and code that you are never confident in.

The best answer is usually converting the times to the smallest units, subtracting that results and converting that number to the original time format.

Like This:

```'Subtract one time from another
time1 = "12:23:58"
time2 = "10:53:14"

time = time1
ConvertTimeToSeconds()
seconds1 = seconds

time = time2
ConvertTimeToSeconds()
seconds2 = seconds

timeLeft = seconds1 - seconds2

seconds = timeLeft
ConvertSecondsToTime()

TextWindow.WriteLine(time1 + " - " + time2 + " = " + time)

Sub ConvertSecondsToTime
hours = Math.Floor(seconds / 3600)
seconds = Math.Remainder(seconds,3600)
minutes = Math.Floor(seconds / 60)
seconds = Math.Remainder(seconds,60)
time = ":" 'needed because small basic refuses to append zeros to an empty string
If hours < 10 Then
time = Text.Append(time,"0")
EndIf
time = time + hours + ":"
If minutes < 10 Then
time = Text.Append(time,"0")
EndIf
time = time + minutes + ":"
If seconds < 10 Then
time = Text.Append(time,"0")
EndIf
time = time + seconds
time = Text.GetSubTextToEnd(time,2) 'Strip out that first :
EndSub

Sub ConvertTimeToSeconds
hours = Text.GetSubText(time,1,2)
seconds = hours * 60 * 60   'convert hours to seconds
minutes = Text.GetSubText(time,4,2)
seconds = seconds + (minutes * 60)  'convert minutes to seconds
seconds = seconds + Text.GetSubText(time,7,2)
EndSub
```

Enjoy!

• Edited by Sunday, May 31, 2015 4:30 PM typos
• Marked as answer by Sunday, May 31, 2015 4:44 PM
Sunday, May 31, 2015 4:10 PM

### All replies

• There are lots of way to performing a calculation like this. The one that usually pops into your head first is treating it like a three digit, base 60 number. I have found that the solution when taking that path is counter-intuitive and always ends up with lots of IF statements and code that you are never confident in.

The best answer is usually converting the times to the smallest units, subtracting that results and converting that number to the original time format.

Like This:

```'Subtract one time from another
time1 = "12:23:58"
time2 = "10:53:14"

time = time1
ConvertTimeToSeconds()
seconds1 = seconds

time = time2
ConvertTimeToSeconds()
seconds2 = seconds

timeLeft = seconds1 - seconds2

seconds = timeLeft
ConvertSecondsToTime()

TextWindow.WriteLine(time1 + " - " + time2 + " = " + time)

Sub ConvertSecondsToTime
hours = Math.Floor(seconds / 3600)
seconds = Math.Remainder(seconds,3600)
minutes = Math.Floor(seconds / 60)
seconds = Math.Remainder(seconds,60)
time = ":" 'needed because small basic refuses to append zeros to an empty string
If hours < 10 Then
time = Text.Append(time,"0")
EndIf
time = time + hours + ":"
If minutes < 10 Then
time = Text.Append(time,"0")
EndIf
time = time + minutes + ":"
If seconds < 10 Then
time = Text.Append(time,"0")
EndIf
time = time + seconds
time = Text.GetSubTextToEnd(time,2) 'Strip out that first :
EndSub

Sub ConvertTimeToSeconds
hours = Text.GetSubText(time,1,2)
seconds = hours * 60 * 60   'convert hours to seconds
minutes = Text.GetSubText(time,4,2)
seconds = seconds + (minutes * 60)  'convert minutes to seconds
seconds = seconds + Text.GetSubText(time,7,2)
EndSub
```

Enjoy!

• Edited by Sunday, May 31, 2015 4:30 PM typos
• Marked as answer by Sunday, May 31, 2015 4:44 PM
Sunday, May 31, 2015 4:10 PM
• You can also do a direct subtraction, with correction for negative values:

```t1="12:34:56"
t2="10:35:57"
hr=Text.GetSubText(t2,1,2)-Text.GetSubText(t1,1,2)
mn=Text.GetSubText(t2,4,2)-Text.GetSubText(t1,4,2)
sc=Text.GetSubText(t2,7,2)-Text.GetSubText(t1,7,2)
If sc<0 Then
mn=mn-1
sc=sc+60
EndIf
sc=Text.GetSubTextToEnd("x0"+sc,1+Text.GetLength(sc))
If mn<0 Then
hr=hr-1
mn=mn+60
EndIf
mn=Text.GetSubTextToEnd("x0"+mn,1+Text.GetLength(mn))+":"
If hr<0 Then
hr=hr+24
EndIf
hr=Text.GetSubTextToEnd("x0"+hr,1+Text.GetLength(hr))+":"
diff=hr+mn+sc
TextWindow.WriteLine(diff)```

Jan [ WhTurner ] The Netherlands

Sunday, May 31, 2015 4:30 PM
• Thankyou very much, this helped me out alot! Great answer :))
Sunday, May 31, 2015 4:44 PM