Link to home
Start Free TrialLog in
Avatar of Dabas
DabasFlag for Australia

asked on

Represent a DateTime Value with tenth of seconds

If it would exist, I would like to be able to use something like

Format(now, "yyyymmdd hh:mm:ss.t")

where t represents tenth of seconds.

Unfortunately Format does not give me this possibility.

I know that a Date Value is actually a double where the fractional part represents the time.
Does this provide accuracy to the tenth of a second?

Or is there any other solution by which I can provide the current time to tenth of second accuracy.

I thought the answer given in

https://www.experts-exchange.com/questions/10046386/Process-Timing.html

would help, but if you look at it carefully, it is based on a falacy and is not acceptable.


Dabas
Avatar of odditysoftware
odditysoftware

how bout this?
Private Sub Timer1_Timer()
  Debug.Print Format(Now, "yyyymmdd hh:mm:ss") & " " & 10 * (Round((Timer - Fix(Timer)), 1))

End Sub
ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dabas

ASKER

crazyman:
You are close. (Your first attempt)

Here is a snippet of what I got when i ran the Debug.Print statement 100 times in a loop
20030716 00:09:28 9
20030716 00:09:28 9
20030716 00:09:28 9
20030716 00:09:28 9
20030716 00:09:28 10
20030716 00:09:28 10
20030716 00:09:28 10
20030716 00:09:28 10
20030716 00:09:29 0
20030716 00:09:29 0
20030716 00:09:29 0
20030716 00:09:29 0

As you can see, it should have gone from 00:09:28 9 to 00:09:29 0 (without the 10)

Solve that and the 500 at yours!

Dabas
try the second one :o)
Avatar of Dabas

ASKER

crazyman:
> try the second one :o)

I did! Thanks!

Dabas
No problemo!
Avatar of Dabas

ASKER

crazyman:
By the way, why fix and not int?

Dabas
To convert datetime into 1/10th of a second you need to do the following :

?format( (((((time  * 24 )- format(time,"h"))* 60) - format(time,"n")) * 60) - format(time,"s"),"0.0")

But this wont return anything meaningfull...

Since Time only returns full seconds...

The alternative solution is timer :)

This returns seconds since midnight...

so

debug.? format(timer - clng(timer),"0.0")

Should output what you need...

cint will return a runtime error after 32000 Seconds of a day passed ... (thats about 9 AM)

thanks for FIX... was thinking what function would return the whole number :)
Avatar of Dabas

ASKER

rdrunner:
Thanks. Too late! Question already answered

odditysoftware: Sorry, but that link was not what I was looking for. Thanks anyways

Dabas
Using Fix or Int in this case shouldnt make any differance, i just used it out of choice...

MSDN
Both Int and Fix remove the fractional part of number and return the resulting integer value.

The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first negative integer greater than or equal to number. For example, Int converts -8.4 to -9, and Fix converts -8.4 to -8.


EG

Dim MyNumber
MyNumber = Int(99.8)   ' Returns 99.
MyNumber = Fix(99.2)   ' Returns 99.

MyNumber = Int(-99.8)   ' Returns -100.
MyNumber = Fix(-99.8)   ' Returns -99.

MyNumber = Int(-99.2)   ' Returns -100.
MyNumber = Fix(-99.2)   ' Returns -99.


hehe no problem...

was trying to figure out why my 1st attempt wasnt working ... took a moment for me to catch that time() only returns time down to the second...

Actually there is a way to display the fractional seconds.  I know that this is a fairly old posting and that a solution has been accepted, but you wanted something even easier, just using the Format function.

THe character you need is 'f', or 'ff' (to show two characters always).

Simple enough.  Format(Now, "HH:mm:ss.ff")

There you go.
Avatar of Dabas

ASKER

wooster11:
Tested OK in .NET, but the problem I had was in VB6.

Unfortunately, unless you are using a reference that I am not, it does not work in VB6
Thanks for revisiting and posting in my question though!

Dabas