VB and VBA Users Source Code: Returning the difference between two times in milliseconds
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Returning the difference between two times in milliseconds
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, April 16, 2008
Hits:
4197
Category:
VBA (Visual Basic for Applications)
Article:
The code below returns the time difference in milliseconds between to times. 'Purpose : Returns the difference between two time variables in ms. 'Inputs : startTime The start time (eg 12:01:49.637) ' endTime The end time (eg 12:01:49.640) 'Outputs : Returns the difference between two time variables in ms. 'Author : Andrew Baker 'Date : 31/12/2000 13:51 'Notes : 'Revisions : Public Function TimeDiffMs(startTime As Variant, endTime As Variant) As Variant Dim startDateTime As Double, endDataTime As Double Dim dStartMs As Double, dEndMs As Double Dim lPos As Long Dim dNumMsInDay As Double On Error GoTo ErrFailed If startTime = endTime Then TimeDiffMs = 0 ElseIf Len(startTime) = 0 Or Len(endTime) = 0 Then TimeDiffMs = 0 Else lPos = InStr(startTime, ".") If lPos Then dStartMs = CDbl(Mid(startTime, lPos + 1)) startTime = Left(startTime, lPos - 1) End If lPos = InStr(endTime, ".") If lPos Then dEndMs = CDbl(Mid(endTime, lPos + 1)) endTime = Left(endTime, lPos - 1) End If dNumMsInDay = 24# * 60# * 60# * 1000# startDateTime = (CDbl(TimeValue(startTime)) * dNumMsInDay) + (dStartMs) endDataTime = CDbl(TimeValue(endTime)) * dNumMsInDay + (dEndMs) TimeDiffMs = endDataTime - startDateTime End If Exit Function ErrFailed: TimeDiffMs = Err.Description Err.Clear End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder