Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Time calculations (with milliseconds)

183 views
Skip to first unread message

Ian

unread,
Feb 1, 2002, 12:24:35 PM2/1/02
to
I'd like to calculate the difference between two times (with milliseconds),
and get the answer back (with milliseconds)

e.g 10:00:00.005 - 10:00:00.001 = 00:00:00.004

Ideas?

Ian


Joe "Nuke Me Xemu" Foster

unread,
Feb 1, 2002, 1:05:18 PM2/1/02
to
"Ian" <ihi...@hotmail.com> wrote in message <news:ewuDkV0qBHA.2332@tkmsftngp03>...

Private Declare Sub GetSystemTime Lib "Kernel32" (lpSystemTime As SYSTEMTIME)
Private Declare Sub GetLocalTime Lib "Kernel32" (lpSystemTime As SYSTEMTIME)

Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type


Public Function RightNow(Optional ByVal UTC As Boolean = False) As Date
Dim ST As SYSTEMTIME
If UTC Then GetSystemTime ST Else GetLocalTime ST

Const ToDate = #12/31/9999# - #12/30/9999#
Const ToHour = #2:00:00 AM# - #1:00:00 AM#
Const ToMinute = #12:01:00 AM# - #12:00:00 AM#
Const ToSecond = #12:00:01 AM# - #12:00:00 AM#
Const ToMilliSec = ToSecond / 1000

RightNow = ((((ST.wMilliseconds * ToMilliSec) + ST.wSecond * ToSecond) _
+ ST.wMinute * ToMinute) + ST.wHour * ToHour) _
+ DateSerial(ST.wYear, ST.wMonth, ST.wDay)
End Function


Public Function FormatInterval(ByVal Interval As Date, _
Optional ByVal ShowDays As Boolean = False) As String

' find the time separator character without hassling with the locale api
Dim TimeSep As String
TimeSep = Mid$(Format$(#11:22:00 AM#, "hh:nn"), 3, 1)

Const SecPerDay = 24# * 3600#
Const DateToSecond = (#12/31/9999# - #12/30/9999#) * SecPerDay

Dim Seconds As Double: Seconds = Interval * DateToSecond

If Seconds < 0 Then FormatInterval = "-": Seconds = -Seconds

' defeat Format$'s rounding
Seconds = Int(Seconds * 10000 + 0.5) / 10000

If ShowDays Then
FormatInterval = FormatInterval & CStr(Int(Seconds / SecPerDay)) & TimeSep
If Seconds >= SecPerDay Then Seconds = Seconds - Int(Seconds / SecPerDay) * SecPerDay
End If

FormatInterval = FormatInterval & Format$(Int(Seconds / 3600), "00")
If Seconds >= 3600 Then Seconds = Seconds - Int(Seconds / 3600) * 3600

FormatInterval = FormatInterval & Format$(Int(Seconds / 60), "\:00")
If Seconds >= 60 Then Seconds = Seconds - Int(Seconds / 60) * 60

FormatInterval = FormatInterval & Format$(Seconds, "\:00.0000")
End Function

--
Joe Foster <mailto:jlfoster%40znet.com> Sign the Check! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!


Ken Halter

unread,
Feb 1, 2002, 1:16:58 PM2/1/02
to
Here's an archived example...

Subject: Re: Better Timer
http://groups.google.com/groups?hl=en&selm=7481o9%24pjf%241%40m5.att.net.hk

--
Ken Halter
MS-MVP-VB
Please keep it in the groups..

"Ian" <ihi...@hotmail.com> wrote in message
news:ewuDkV0qBHA.2332@tkmsftngp03...

Joe "Nuke Me Xemu" Foster

unread,
Feb 1, 2002, 4:35:23 PM2/1/02
to
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in message <news:emKymy0qBHA.600@tkmsftngp04>...

> Here's an archived example...
>
> Subject: Re: Better Timer
> http://groups.google.com/groups?hl=en&selm=7481o9%24pjf%241%40m5.att.net.hk

Why not just use the QueryPerformance APIs?

URL:http://groups.google.com/groups?selm=01bd5190%246adcfd10%24ae09d9c2%40mnvqc

Michael Dunn

unread,
Feb 2, 2002, 12:50:18 PM2/2/02
to
If you just want the result for display purposes, here's a really weird way of doing it

Private Sub Command1_Click()
Dim Time1() As String
Dim Time2() As String
Dim TimeDiff As String

Time1() = Split("10:00:02.005", ":")
Time2() = Split("10:00:00.011", ":")
TimeDiff = (Time1(0) * 3600000 + Time1(1) * 60000 + Time1(2) * 1000) _
- (Time2(0) * 3600000 + Time2(1) * 60000 + Time2(2) * 1000)
TimeDiff = Format((TimeDiff \ 1000) / 86400, "hh:nn:ss") _
& Format((TimeDiff Mod 1000) / 1000, ".000")
MsgBox TimeDiff
End Sub


--
Regards

Michael Dunn

"Ian" <ihi...@hotmail.com> wrote in message news:ewuDkV0qBHA.2332@tkmsftngp03...

0 new messages