Solved

Calculate datediff using SystemTime

Posted on 2007-11-27
8
1,623 Views
Last Modified: 2010-04-21
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
Comment
Question by:sidwelle
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 20360266
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20360316
if you just want the difference in miliseconds, you can use the WinAPI GetTickCount()
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20360345
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
 
LVL 81

Expert Comment

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

Kevin
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 20360415
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 20360450
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
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 20360560
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
 

Author Closing Comment

by:sidwelle
ID: 31427345
Thanks Paul,
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
clicking a shape in a frame array vb6 3 40
Child Form in front 4 37
VB6 ListBox Question 4 32
Problem to With line 4 39
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now