Excel VBA中的时间计算

时间:2014-08-27 11:13:20

标签: vba excel-vba time excel-2003 excel

我的时间为23300000,即hhMMssmm格式为字符串 我想计算这两个值的差异。

这里hh是小时,MM是分钟,ss是秒,mm是秒的60。 使用VBA for Excel 2003

3 个答案:

答案 0 :(得分:1)

UDF 将返回

中差异的绝对值
Public Function tDiff(s1 As String, s2 As String) As Double
'
'   calculates the absolute value of the differences
'   returns the answer in seconds
'
    Dim hrs As Double, mins As Double, secs As Double, sixt As Double
    Dim tVal1 As Double, tVal2 As Double

    hrs = CDbl(Mid(s1, 1, 2)) * 60 * 60
    mins = CDbl(Mid(s1, 3, 2)) * 60
    secs = CDbl(Mid(s1, 5, 2))
    sixt = CDbl(Mid(s1, 7, 2)) / 60
    tVal1 = hrs + mins + secs + sixt

    hrs = CDbl(Mid(s2, 1, 2)) * 60 * 60
    mins = CDbl(Mid(s2, 3, 2)) * 60
    secs = CDbl(Mid(s2, 5, 2))
    sixt = CDbl(Mid(s2, 7, 2)) / 60
    tVal2 = hrs + mins + secs + sixt

    If tVal1 > tVal2 Then
        tDiff = tVal1 - tVal2
    Else
        tDiff = tVal2 - tVal1
    End If
End Function

答案 1 :(得分:1)

这样的事情怎么样:

Public Sub test()
    Dim ms1 As Double
    Dim ms2 As Double

    ms1 = ToSeconds(23142700)
    ms2 = ToSeconds(23311500)

    Debug.Print "Difference between dates in seconds: " & ms2 - ms1
End Sub


Public Function ToSeconds(number As Long) As Double
    Dim hh As Long
    Dim mm As Long
    Dim ss As Long
    Dim ms As Long

    ms = (number Mod (100 ^ 1)) / (100 ^ 0)
    ss = (number Mod (100 ^ 2) - ms) / (100 ^ 1)
    mm = (number Mod (100 ^ 3) - ss * (100 ^ 1) - ms) / (100 ^ 2)
    hh = (number Mod (100 ^ 4) - mm * (100 ^ 2) - ss * (100 ^ 1) - ms) / (100 ^ 3)

    ToSeconds = ms * 1 / 60 + ss + mm * 60 + hh * 60 * 60
End Function

ToSeconds()函数将您的数字转换为秒,您可以根据它进行计算。

答案 2 :(得分:0)

虽然这个解决方案可能没有其他解决方案那么短,但我相信它很容易理解。这里不是所有的东西都可能是必要的,但你可能会发现它有一些用处。

run子例程允许您使用指定的值运行test函数 test函数测试timeDiff& timeSum逻辑 timeDiff函数查找t1t0之间的时差 timeSum函数查找t1t0的时间总和 asDuration函数从时间值中删除AM / PM后缀 asMilitary函数将12小时格式转换为24小时格式 我创建的concat函数更容易连接字符串。

Sub Main() 'Run Test
    MsgBox Test("0:29:0", "23:30:0")
End Sub

Function Test(startT As Date, endT As Date) 'Test timeDiff & timeSum logic
    Dim nextShift As Date, prevShift As Date, hours As Date

    hours = timeDiff(endT, startT)
    prevShift = timeDiff(startT, "0:30:0")
    nextShift = timeSum("0:30:0", endT)        

    Test = concat("Start -", startT, "", "End - ", endT, "", "Duration -", asDuration(hours), "", "Next Shift: ", nextShift, "", "Prev Shift: ", prevShift)
End Function


Function timeDiff(t1 As Date, t0 As Date) As Date 'Return Time1 minus Time0
    Dim units(0 To 2) As String

    units(0) = Hour(t1) - Hour(t0)
    units(1) = Minute(t1) - Minute(t0)
    units(2) = Second(t1) - Second(t0)

    If units(2) < 0 Then
        units(2) = units(2) + 60
        units(1) = units(1) - 1
    End If

    If units(1) < 0 Then
        units(1) = units(1) + 60
        units(0) = units(0) - 1
    End If

    units(0) = IIf(units(0) < 0, units(0) + 24, units(0))
    timeDiff = Join(units, ":")
End Function


Function timeSum(t1 As Date, t0 As Date) As Date 'Return Time1 plus Time0
    Dim units(0 To 2) As String

    units(0) = Hour(t1) + Hour(t0)
    units(1) = Minute(t1) + Minute(t0)
    units(2) = Second(t1) + Second(t0)

    If units(2) >= 60 Then
        units(2) = units(2) Mod 60
        units(1) = units(1) + 1
    End If

    If units(1) >= 60 Then
        units(1) = units(1) Mod 60
        units(0) = units(0) + 1
    End If

    units(0) = IIf(units(0) >= 24, units(0) Mod 24, units(0))
    timeSum = Join(units, ":")
End Function


Function asDuration(time As Date) As String 'Display as duration; Remove AM/PM suffix from time
    time = asMilitary(time)
    asDuration = Left(time, Len(time))
End Function


Function asMilitary(time As Date) As String 'Convert 12-hour format to 24-hour-format
    asMilitary = Hour(time) & ":" & Minute(time) & ":" & Second(time)
End Function


Function concat(ParamArray var() As Variant) As String 'Return arguments of function call concatenated as a single string
    For Each elem In var()
        concat = IIf(elem <> "", concat & elem & " ", concat & vbNewLine)
    Next
End Function