仅使用VB6 ......
我有两次,需要在他们之间抽出时间。我有一个文件,其中包含"08:34:45:734"
格式的时间,我只需要计算两个值之间的时差。
示例:
08:34:12:744
08:34:45:734
我需要输出来生成两次之间的正确时间,包括毫秒。我试图转换为double和subtract,但我似乎无法一直恢复到我需要的H:M:S:m
时间格式。
答案 0 :(得分:1)
这应该有效。 VB日期不包括ms的数量,但你可以直接删除ms并自己计算差异。
Private Function GetTimeDiff(strDate1 As String, strDate2 As String) As String
Dim fNeg As Boolean
Dim dt1 As Date, dt2 As Date
Dim ms1 As Long, ms2 As Long
' Assign earliest date to dt1...
If strDate1 <= strDate2 Then
dt1 = Left$(strDate1, 8)
dt2 = Left$(strDate2, 8)
ms1 = Right$(strDate1, 3)
ms2 = Right$(strDate2, 3)
Else
dt1 = Left$(strDate2, 8)
dt2 = Left$(strDate1, 8)
ms1 = Right$(strDate2, 3)
ms2 = Right$(strDate1, 3)
fNeg = True
End If
' If ms of starting time > ms of ending time, add a second before subtraction...
Dim ms As Long
If ms1 > ms2 Then
dt1 = DateAdd("s", 1, dt1)
ms = (1000 - ms1) + ms2
Else
ms = ms2 - ms1
End If
' Subtract dates, get difference in seconds...
Dim h As Long, m As Long, s As Long
s = DateDiff("s", dt1, dt2)
' Convert seconds to H:M:S...
h = s \ 3600
s = s Mod 3600
m = s \ 60
s = s Mod 60
GetTimeDiff = IIf(fNeg, "-", "") & h & ":" & m & ":" & s & ":" & ms
End Function
通过传递字符串值来调用它,它将返回h:m:s:ms
字符串:
Debug.Print GetTimeDiff("08:34:12:744", "08:34:45:734") ' => "0:0:32:990"
如果第一个日期晚于第二个日期,您将获得一个负值:
Debug.Print GetTimeDiff("10:34:12:744", "08:34:45:734") ' => "-1:59:27:10"
答案 1 :(得分:1)
我对此的尝试试图允许标志指示符和结果包含在几天内(超过23:59:59:999或小于-23:59:59:999)。如果这些对您来说不是问题,那么您可以忽略标志指示符和日期,因为两者都是&#34; +&#34;结果中的零日被抑制。
Private Function ToLong(ByVal HMSms As String) As Long
'Valid value range is -2:20:31:23:647 to +2:20:31:23:647,
'Sign indicator optional, days position optional.
Dim Parts() As String
Dim SChar As String
Dim Sign As Long
SChar = Left$(HMSms, 1)
If SChar = "+" Or SChar = "-" Then
HMSms = Mid$(HMSms, 2)
Sign = IIf(SChar = "+", 1, -1)
Else
Sign = 1
End If
Parts = Split(HMSms, ":")
If UBound(Parts) < 4 Then
Parts = Split("0:" & HMSms, ":")
End If
ToLong = CLng(Parts(4)) _
+ CLng(Parts(3)) * 1000& _
+ CLng(Parts(2)) * 1000& * 60& _
+ CLng(Parts(1)) * 1000& * 60& * 60& _
+ CLng(Parts(0)) * 1000& * 60& * 60& * 24&
ToLong = ToLong * Sign
End Function
Private Function Diff(ByVal Minu As String, ByVal Subtra As String) As String
'Result suppresses positive sign indicator and 0 value in days position.
Dim DiffLong As Long
Dim SChar As String
DiffLong = ToLong(Minu) - ToLong(Subtra)
If DiffLong < 0 Then
SChar = "-"
DiffLong = -DiffLong
End If
Diff = "0:00:00:00:000"
Mid$(Diff, 1, 1) = Format$(DiffLong \ (1000& * 60& * 60& * 24&), "0")
Mid$(Diff, 3, 2) = Format$(DiffLong \ (1000& * 60& * 60&) Mod 24&, "00")
Mid$(Diff, 6, 2) = Format$(DiffLong \ (1000& * 60&) Mod 60&, "00")
Mid$(Diff, 9, 2) = Format$(DiffLong \ 1000& Mod 60&, "00")
Mid$(Diff, 12, 3) = Format$(DiffLong Mod 1000&, "000")
If Left$(Diff, 1) = "0" Then Diff = Mid$(Diff, 3)
Diff = SChar & Diff
End Function
一些测试用例结果:
08:34:45:734 - 08:34:12:744 = 00:00:32:990
08:34:45:734 - 08:34:40:734 = 00:00:05:000
08:34:12:744 - 08:34:45:734 = -00:00:32:990
-00:00:00:000 - +00:00:00:000 = 00:00:00:000
+00:00:00:000 - 00:00:00:001 = -00:00:00:001
1:00:00:00:000 - 00:00:00:001 = 23:59:59:999
-2:00:00:00:000 - 00:00:00:000 = -2:00:00:00:000
-12:00:00:000 - 12:00:00:000 = -1:00:00:00:000
-23:59:59:999 - 00:00:00:000 = -23:59:59:999