It first brakes the two numbers, the two numbers you want to add into hours, minutes and seconds. Then It adds them together (each part by its own), it makes sure that the result is two characters, because if you add 2 and 2 VB will consider it as 4 and not as 04. After that it makes sure that the seconds. and the hours are not more then 60 if they are it adds one to the hours or the minutes, depends on what it is, if it is seconds. then it adds one to the minutes if it is minutes it adds one to the hours. Then it puts the results in a time format (hh:mm:ss).
Code
Function MakeTotalTime(First, Second)
'Divides the two time format numbers into parts:
'Hours, Minutes And Seconds.
Firsth = Left(First, 2)
Firstm = Mid(First, 4, 2)
Firsts = Right(First, 2)
Secondh = Left(Second, 2)
Secondm = Mid(Second, 4, 2)
Seconds = Right(Second, 2)
'----------------------------
' Adds the numbers and makes sure that it will
' always be with two characters
uth = Val(Firsth) + Val(Secondh)
If Len(uth) = 1 Then uth = "0" & uth
utm = Val(Firstm) + Val(Secondm)
If Len(utm) = 1 Then utm = "0" & utm
uts = Val(Firsts) + Val(Seconds)
If Len(uts) = 1 Then uts = "0" & uts
'----------------------------
' If the Seconds are higher than 60 then add
' one to the Minutes and subtract from the seconds
' 60
If uts > 59 Then
Do
uts = Val(uts) - 60
If Len(uts) = 1 Then uts = "0" & uts
utm = Val(utm) + 1
If Len(utm) = 1 Then utm = "0" & utm
Loop Until uts <= 59
End If
'----------------------------
' If the Minutes are higher than 60 then add
' one to the Hours and subtract from the Minutes
' 60
If utm > 59 Then
Do
utm = Val(utm) - 60
If Len(utm) = 1 Then utm = "0" & utm
uth = Val(uth) + 1
If Len(uth) = 1 Then uth = "0" & uth
Loop Until utm <= 59
End If
'----------------------------
' Returns the Total Time
newTotal = uth & ":" & utm & ":" & uts
MakeTotalTime = newTotal
End Function
Tip Submitted By: Yatir Halevi
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
