Adding Time

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s