Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate timedifference in vb6

Tags:

vb6

I want to calculate time difference in vb6. i had been done it in vb.net. but i don't know how to convert in vb6.Because the datetime type will not be available in vb6.

i have pasted my vb.net code below. how to do the same in vb6?

Public Sub timecal()
    Dim dFrom As DateTime
    Dim dTo As DateTime
    Dim tempstarttime As DateTime
    Dim idletime As String = "00:05:00"
    Dim Needtosub As String = "00:01:00"
    Dim timeDiff As String
    If DateTime.TryParse(start_time, dFrom) AndAlso DateTime.TryParse(end_time, dTo) Then
        Dim TS As TimeSpan = dTo - dFrom
        Dim hour As Integer = TS.Hours
        Dim mins As Integer = TS.Minutes
        Dim secs As Integer = TS.Seconds
        timeDiff = ((hour.ToString("00") + ":") + mins.ToString("00") + ":") + secs.ToString("00")
        sscheck1 = False
        If timeDiff >= idletime Then
            tempstarttime = System.DateTime.Parse(end_time)
            tempstarttime = tempstarttime.AddMinutes(-1)
            start_time = Strings.Format(tempstarttime, "yyyy-MM-dd HH:mm:ss")
            sscheck1 = True
        End If
    End If
End Sub
like image 954
user3141349 Avatar asked Dec 05 '25 19:12

user3141349


1 Answers

You can calculate the time difference between two Date values (which will include the time) using the DateDiff function

For difference in minutes:

Dim lMinutes as Long
lMinutes = DateDiff("n", dFrom, dTo)

For difference in seconds:

Dim lSeconds as Long
lSeconds = DateDiff("s", dFrom, dTo)

For difference in hours:

Dim lHours as Long
lHours = DateDiff("h", dFrom, dTo)

For the purposes of getting your string time diff in hours:minutes:seconds, I would do:

Dim lSeconds as Long, lMinutes as Long
lSeconds = DateDiff("s", dFrom, dTo)
lMinutes = Fix(lSeconds / 60) '// Gets the whole number (not rounded)
lSeconds = ((lSeconds / 60) - lMinutes) * 60 '// get the remaining seconds
sTimeDiff = "00:" & Format$(lMinutes, "00") & ":" & Format$(lSeconds, "00")

Note: If lMinutes is greater than 60, you'll need to do similar math to pull out the hours portion before pulling the minutes and seconds. This code assumes your timespan is less than an hour based on your example.

like image 109
C-Pound Guru Avatar answered Dec 08 '25 09:12

C-Pound Guru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!