[VB.NET] Time difference and summation of time


Description
       this article will help you you , how to summation of time and time diference between 2 time, of course you can modified based on what you need. this example below using 12h format, but will return 24h format.

HowTo
time 1 : 09:30:30 AM (it's string use textbox) as lowtime
time 2 : 07:15:15 PM (it's string use textbox) as hightime

Here's the function for time difference

    Public Function timeDifference(ByVal lowtime As Date, ByVal hightime As Date)
        Dim hours, minutes, seconds
        hours = CStr(Math.Floor((hightime - lowtime).TotalHours)).PadLeft(2, "0")
        minutes = CStr(Math.Floor(((hightime - lowtime).TotalMinutes) Mod 60)).PadLeft(2, "0")
        seconds = CStr(Math.Floor(((hightime - lowtime).TotalSeconds) Mod 60)).PadLeft(2, "0")
        MsgBox(hours & ":" & minutes & ":" & seconds)
    End Function


Here's the function for summation of time

    Public Function summationOfTime(ByVal lowtime As String, ByVal hightime As String)
        Dim timespanLowtime, timespanHightime, timespanSum As TimeSpan
        timespanLowtime = TimeSpan.Parse(lowtime)
        timespanHightime = TimeSpan.Parse(hightime)
        timespanSum = timespanLowtime + timespanHightime
        MsgBox(timespanSum.ToString)
    End Function

don't worry , the function above is well format :D

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih