Re: Constructing a more precise "Now()" and "DateDiff()"
- From: "Tony Proctor" <tony_proctor@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 3 May 2006 11:09:07 +0100
I haven't really had any problems with the 1-sec resolution of Now() Bob.
However, I often supplement it with the following function to format
milliseconds on the end for timestamps:
Public Function sFormatNowMS(Optional sFmt As String = "ddddd ttttt.fff") As
String
' Formats 'now' as a text string down to millisecond resolution (approx).
' The format string will accept ".fff" for milliseconds, the same as VB.Net.
' The default format string will generate a full date/time incorporating
milliseconds.
Dim dNow As Date, lMS As Long
' Get normal date/time, to seconds resolution, and milliseconds from
' midnight. NB: must be in synch
Do While dNow <> Now()
dNow = Now(): lMS = CLng(Timer() * 1000)
Loop
sFormatNowMS = Format(dNow, sFmt)
sFormatNowMS = Replace(sFormatNowMS, ".fff", _
Format(lMS Mod 1000, "\.000"))
End Function
This is a VB-only non-API variation of what others have suggested. Note the
way it ensures Now() and Timer() are synchronised to avoid potential hiccups
at 1-sec boundaries.
Someone once pointed out that the default format string isn't very useful in
the US. It seems you guys use a 12-hour clock more than we do here. With a
24-hour clock setting it works fine :-)
Tony Proctor
"Bob O`Bob" <filterbob@xxxxxxxxxxxxxxx> wrote in message
news:e0iQySibGHA.864@xxxxxxxxxxxxxxxxxxxxxxx
Has anyone already done some work (or even just research) in this area?Timer & vbCrLf
A trivial demo app can show that Now() isn't very precise:
Option Explicit
Private Sub Form_Resize()
Text1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub
Private Sub Text1_DblClick()
Text1.Text = Text1.Text & Format(Now, "general number") & vbTab &
End Subthat
Paste that code into a form with one textbox, and click away.
You'll see that return values from Now() can repeat, perhaps for
up to a whole second.
While Timer() is more precise, it's based on boot time, so correlating
to something which could be meaningful outside of one computer is a bit ofcurrency
challenge.
What I'd really like to have is a function which returns the number of
/hundredths of/ a second which have passed since some specific base time.
I'd prefer the beginning of #1/1/1970# - but /that/ won't fit in a Long.
A currency type could work, AND to take advantage of its built-in scaling,
having it indicate seconds would be great. So a function returning a
type with the number of seconds (to the .01 of a second) since #1/1/1970#is
(at least at this moment) my ideal.my needs.
Of course there are innumerable alternatives which would be just fine for
Maybe a Long holding hundredths of seconds since local midnight.Now()
Or even just something returning a Date that's merely more precise than
already has,
Before I start in on anything like that, I thought I'd ask if anyone
or if maybe there's an existing API I should look into.
Bob
--
.
- References:
- Constructing a more precise "Now()" and "DateDiff()"
- From: Bob O`Bob
- Constructing a more precise "Now()" and "DateDiff()"
- Prev by Date: Re: VB open excel
- Next by Date: Re: VB open excel
- Previous by thread: Re: Constructing a more precise "Now()" and "DateDiff()"
- Next by thread: Re: Constructing a more precise "Now()" and "DateDiff()"
- Index(es):
Relevant Pages
|