This function will return the span of time between two times with the assumption that the total span is with in 24hrs.
CODE
Public Function TimeDiff(strInterval As String, _
dtStartTime As Date, _
dtStopTime As Date) As Long
TimeDiff = DateDiff(strInterval, #12:00:00 AM#, _
Format(TimeValue(dtStartTime) - 1 - TimeValue(dtStopTime), _
"hh:nn:ss"))
End Function
dtStartTime As Date, _
dtStopTime As Date) As Long
TimeDiff = DateDiff(strInterval, #12:00:00 AM#, _
Format(TimeValue(dtStartTime) - 1 - TimeValue(dtStopTime), _
"hh:nn:ss"))
End Function
strInterval can be any span identifier used in DateDiff() function, but is only makes sense to use "h","n", or "s".
dtStartTime is a date/time that indicates the start time of the span.
dtStopTime is a date/time that indicates the stop time of the span.
... Important note, the start time is ALWAYS assumed to be LESS than the stop time, so you will not get a negative value returned from this function ...
Examples from the immediate window (us format on dates ... but this code *should* work with different regional settings for dates as well):
? TimeDiff("n", #4/3/2008 11:55 PM#, #4/4/2008 12:05 AM#)
10
? TimeDiff("n", #1/1/1900 11:55 PM#, #4/4/2120 12:05 AM#)
10
Now ... lets reverse the start and stop dates ....
? TimeDiff("n", #4/4/2008 12:05 AM#, #4/3/2008 11:55 PM#)
1430
... So ... as evidenced by the results, the start time is assumed to be less than the stop time, regardless of the date value tagged to the time ...
Enjoy! ...
{Please comment via PM if issues arise with this code}