如何计算vb6中的时差

时间:2014-08-06 06:30:29

标签: vb6

我想计算vb6的时差。我是在vb.net上完成的。但我不知道如何在vb6中进行转换。因为日期时间类型在vb6中不可用。

我已粘贴下面的vb.net代码。如何在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

2 个答案:

答案 0 :(得分:7)

您可以使用DateDiff function

计算两个Date值之间的时差(包括时间)

分钟差异:

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

以秒为单位的差异:

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

以小时差异:

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

为了让你的字符串时间差异以小时:分钟:秒为单位,我会这样做:

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")

注意:如果lMinutes大于60,那么在拉动分钟和秒数之前,您需要进行类似的数学计算以拉出小时部分。根据您的示例,此代码假定您的时间跨度不到一小时。

答案 1 :(得分:3)

使用vb DateDiff函数,即

Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week. 
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)

或者可以用另一种通用方式完成

    Dim dtStart As Long
    Dim dtEnd As Long
    Dim result As Long
    Dim i As Integer

    dtStart = GetTickCount

    dtEnd = GetTickCount
    result = dtEnd – dtStart  // miliseconds 
   OutAddIn.objApptItem.Start = CDbl(result)   // miliseconds to date