# Re: 两位之解答都还是时间，不是时间差或和！

Posted by 小v on July 07, 1999 at 15:54:47:

In Reply to: 两位之解答都还是时间，不是时间差或和！ posted by 新手 on July 07, 1999 at 01:15:14:

: 最后答案应该是例如：
: T1=16:30:20
: T2=9:10:10
: T3=T1-T2
: T3=7:20:10
: 时间差为7时20分10秒，而非AM 7:20:10 或是PM

手边有个函数TimeDiffs()给你参考!
若有问题请来信告知!

Public Function TimeDiffS(ByVal StartTim As Variant, ByVal EndTim As Variant) As String
Dim T1 As Long, T2 As Long
'计算两个时间字串(格式为hh:mm:ss)相隔的时间(hh:mm:ss)
If TypeName(StartTim) = "String" Then StartTim = CDate(StartTim)
If TypeName(EndTim) = "String" Then EndTim = CDate(EndTim)
T1 = Time2Sec(StartTim)
T2 = Time2Sec(EndTim)
If T2 < T1 Then T2 = T2 + 86400
TimeDiffS = Sec2Time(T2 - T1)
End Function

Public Function Time2Sec(ByVal TimStr As Variant) As Long
Dim HH As Long, mM As Long, ss As Long
'将时间字串(格式必须是hh:mm:ss)化成秒数
If TypeName(TimStr) = "String" Then
TimStr = CDate(TimStr)
End If
HH = Hour(TimStr)
mM = Minute(TimStr)
ss = Second(TimStr)
Time2Sec = (HH * 3600) + (mM * 60) + ss
End Function

Public Function Sec2Time(ByVal nSec As Variant) As String
Dim HH As Long, mM As Long, ss As Long
'将秒数化成时间字串(格式为hh:mm:ss)
HH = Fix(nSec / 3600)
nSec = nSec - (HH * 3600)
mM = Fix(nSec / 60)
nSec = nSec - (mM * 60)
ss = nSec
HH = HH Mod 24
Sec2Time = StrZero(HH, 2) + ":" + StrZero(mM, 2) + ":" + StrZero(ss, 2)
End Function

Follow Ups: