学员问题: 如何计算两个时间间隔并转换成“N天N小时N分N秒”这样的形式?
这里用函数解决
Function timeDiff(dteStart As Date, dteEnd As Date) As String
Dim lngDiff As Long
Dim strCheck(3)
lngDiff = DateDiff("s", dteStart, dteEnd)
strCheck(0) = CStr(lngDiff Mod 60) & "秒"
strCheck(1) = CStr(lngDiff \ 60 Mod 60) & "分"
strCheck(2) = CStr(lngDiff \ 60 \ 60 Mod 24) & "小时"
strCheck(3) = CStr(lngDiff \ 60 \ 60 \ 24) & "天"
Dim i As Integer
For i = 0 To 3
If Left(strCheck(i), 1) = "0" Then
strCheck(i) = ""
End If
Next
timeDiff = strCheck(3) & strCheck(2) & strCheck(1) & strCheck(0)
End Function
调用示例 :
Sub test1()
Debug.Print timeDiff(#6/18/2004 1:01:51 AM#, #6/28/2004 1:01:52 AM#)
End Sub