Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1652
  • Last Modified:

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
Asked:
sidwelle
  • 3
  • 2
  • 2
  • +1
1 Solution
 
zorvek (Kevin Jones)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
 
Jaime OlivaresCommented:
if you just want the difference in miliseconds, you can use the WinAPI GetTickCount()
0
 
Jaime OlivaresCommented:
Declare Function GetTickCount Lib "kernel32" () As Long

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

    t0 = GetTickCount()
    ' Do some task here
    t1 = GetTickCount()

diff = t1 - t0   ' difference in miliseconds
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
zorvek (Kevin Jones)ConsultantCommented:
The Windows SDK function GetTickCount has the same accuracy as the VB Timer function. I believe the Timer function uses GetTickCount.

Kevin
0
 
zorvek (Kevin Jones)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
 
PaulHewsCommented:
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

Open in new window

0
 
PaulHewsCommented:
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

Open in new window

0
 
sidwelleAuthor Commented:
Thanks Paul,
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now