• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1681

# Calculate datediff using SystemTime

I need to calculate the difference in times down to the milliSecond.
DateDiff won't accept SystemTime so I wanted a native function to calc datediff down to the ms.

Thanks
0
sidwelle
• 3
• 2
• 2
• +1
1 Solution

ConsultantCommented:
If you are looking for the delta time between two points in your code use the VB Timer function. it returns a Single representing the number of seconds elapsed since midnight. It is accurate down to about 1/100 of a second.

Kevin
0

Software ArchitectCommented:
if you just want the difference in miliseconds, you can use the WinAPI GetTickCount()
0

Software ArchitectCommented:
Declare Function GetTickCount Lib "kernel32" () As Long

Dim t0 as Long
Dim t1 as Long
Dim diff as Long

t0 = GetTickCount()
t1 = GetTickCount()

diff = t1 - t0   ' difference in miliseconds
0

ConsultantCommented:
The Windows SDK function GetTickCount has the same accuracy as the VB Timer function. I believe the Timer function uses GetTickCount.

Kevin
0

ConsultantCommented:
Here is a short article on getting better timing on Windows using VB and VB.net:

http://gpwiki.org/index.php/VB:Timers

Also note the following:

Windows NT 3.5 and later The system timer runs at approximately 10ms.
Windows NT 3.1 The system timer runs at approximately 16ms.
Windows 95 and later The system timer runs at approximately 55ms.

Kevin
0

Commented:
Find difference in milliseconds between two systemtime variables:

``````Private Function GetDiffMilliseconds(Second As SYSTEMTIME, First As SYSTEMTIME) As Long
'This function assumes the second date is the greater in'
'order to return positive number differences.'
'Returns number of milliseconds difference.

Dim Ret As Long
Dim dtFirst As Date, dtSecond As Date

dtFirst = DateSerial(First.wYear, First.wMonth, First.wDay) + TimeSerial(First.wHour, First.wMinute, First.wSecond)
dtSecond = DateSerial(First.wYear, First.wMonth, First.wDay) + TimeSerial(First.wHour, First.wMinute, First.wSecond)

Ret = Second.wMilliseconds - First.wMilliseconds
Ret = DateDiff("s", dtFirst, dtSecond) * 1000 + Ret

GetDiffMilliseconds = Ret
End Function
``````
0

Commented:
Woops, a couple of fixes.... Fix a typo and an overflow problem.
``````Private Function GetDiffMilliseconds(Second As SYSTEMTIME, First As SYSTEMTIME) As Double
'This function assumes the second date is the greater in'
'order to return positive number differences.'
'Returns number of milliseconds difference.'

Dim Ret As Double
Dim dtFirst As Date, dtSecond As Date

dtFirst = DateSerial(First.wYear, First.wMonth, First.wDay) + TimeSerial(First.wHour, First.wMinute, First.wSecond)
dtSecond = DateSerial(Second.wYear, Second.wMonth, Second.wDay) + TimeSerial(Second.wHour, Second.wMinute, Second.wSecond)

Ret = Second.wMilliseconds - First.wMilliseconds
Ret = DateDiff("s", dtFirst, dtSecond) * CDbl(1000) + Ret

GetDiffMilliseconds = Ret
End Function
``````
0

Author Commented:
Thanks Paul,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.