Board index » Visual Studio » How to write out current time including milliseconds?

How to write out current time including milliseconds?

Visual Studio329
I tried format$(now, "hhh:mm:ss.xxx"), where xxx is pretty much any

letter out there all to no avail.



How to write out current time including milliseconds in the format

specified?


-
 

Re:How to write out current time including milliseconds?



"Frank Rizzo" <none@none.net>wrote in message

Quote
I tried format$(now, "hhh:mm:ss.xxx"), where xxx is pretty much any letter

out there all to no avail.



How to write out current time including milliseconds in the format

specified?



Precision of the Date data type is limited to seconds. You should use WinAPI

for better precision. The following sample is from API-Guide



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

Private Sub Form_Load()

'KPD-Team 1998

'URL: www.allapi.net/">www.allapi.net/

'E-Mail: KPDTeam@Allapi.net

Dim MyTime As SYSTEMTIME

'Set the graphical mode to persistent

Me.AutoRedraw = True

'Get the local time

GetLocalTime MyTime

'Print it to the form

Me.Print "The Local Date is:" & MyTime.wMonth & "-" & MyTime.wDay & "-"

& MyTime.wYear

Me.Print "The Local Time is:" & MyTime.wHour & ":" & MyTime.wMinute &

":" & MyTime.wSecond & "." & MyTime.wMilliseconds

End Sub





Dmitriy.





-

Re:How to write out current time including milliseconds?

"Dmitriy Antonov" <antonovdima@netzero.net_not_for_spam>wrote in message

Quote


"Frank Rizzo" <none@none.net>wrote in message

news:eYxxFufmIHA.6064@TK2MSFTNGP03.phx.gbl...

>I tried format$(now, "hhh:mm:ss.xxx"), where xxx is pretty much any

letter

>out there all to no avail.

>

>How to write out current time including milliseconds in the format

>specified?



Precision of the Date data type is limited to seconds. You should use

WinAPI

for better precision. The following sample is from API-Guide



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

Private Sub Form_Load()

'KPD-Team 1998

'URL: www.allapi.net/">www.allapi.net/

'E-Mail: KPDTeam@Allapi.net

Dim MyTime As SYSTEMTIME

'Set the graphical mode to persistent

Me.AutoRedraw = True

'Get the local time

GetLocalTime MyTime

'Print it to the form

Me.Print "The Local Date is:" & MyTime.wMonth & "-" & MyTime.wDay &

"-"

& MyTime.wYear

Me.Print "The Local Time is:" & MyTime.wHour & ":" & MyTime.wMinute &

":" & MyTime.wSecond & "." & MyTime.wMilliseconds

End Sub





Dmitriy.





Curiosity, is the API ever more accurate than simply 'Timer'.



Regards,

Peter T





-

Re:How to write out current time including milliseconds?



"Peter T" <peter_t@discussions>wrote in message

Quote
"Dmitriy Antonov" <antonovdima@netzero.net_not_for_spam>wrote in message

news:ea631GgmIHA.1912@TK2MSFTNGP04.phx.gbl...

>



Curiosity, is the API ever more accurate than simply 'Timer'.



Regards,

Peter T







I am not sure about accuracy of this particular API function. I heard that

the Timer function is updated 18+ times per second. The sample I provided is

not the best one either but I guess it is still more accurate then the Timer

function. If highest possible precision is interesting then you may look at

multimedia timers - there are plenty of info about it on the Internet.



Dmitriy.





-

Re:How to write out current time including milliseconds?

"Peter T" <peter_t@discussions>wrote in message



Quote
Curiosity, is the API ever more accurate than simply 'Timer'.



If you mean accuracy in terms of the actual real world time then none of the

methods is particularly accurate, and certainly not to within a few

milliseconds, even if your machine is regularly updated from a reliable time

source. But if you mean resolution, then the multimedia timer will give you

a resolution of about one millisecond and QueryFormanceCounter will give you

a resolution of about one micosecond, provided you take care to ignore the

occasional spurious timing it will throw up very occasionally on some

systems under certain conditions.



Mike





-

Re:How to write out current time including milliseconds?

"Dmitriy Antonov" <antonovdima@netzero.net_not_for_spam>wrote in message

Quote


"Peter T" <peter_t@discussions>wrote in message



>"Dmitriy Antonov" <antonovdima@netzero.net_not_for_spam>wrote in

message

>

>>



>Curiosity, is the API ever more accurate than simply 'Timer'.

>



I am not sure about accuracy of this particular API function. I heard that

the Timer function is updated 18+ times per second.>



So it does indeed, and about the same as the GetLocalTime it seems, at least

for me. Enough for many purposes but not milliseconds.



Public Declare Function GetTickCount Lib "kernel32" () As Long



Sub test()

Dim tmr As Single, tmrStop As Single

Dim t1 As Long, t2 As Long



tmrStop = Timer + 2



Do While Timer < tmrStop



If tmr <>Timer Then

tmr = Timer

t2 = GetTickCount

Debug.Print tmr, t2, Int(1000 / (t2 - t1)) ' updates / sec

t1 = t2

End If

Loop



about 18 times per second



Regards,

Peter T





-

Re:How to write out current time including milliseconds?

"Mike Williams" <mikea@whiskyandCoke.com>wrote in message

Quote
"Peter T" <peter_t@discussions>wrote in message



>Curiosity, is the API ever more accurate than simply 'Timer'.



If you mean accuracy in terms of the actual real world time then none of

the

methods is particularly accurate, and certainly not to within a few

milliseconds, even if your machine is regularly updated from a reliable

time

source.



I guess what I really meant to ask was - is using the GetLocalTime API any

more accurate than 'Timer' to return "current time" or fractional seconds

since midnight. In a light test they both seem about the same, ie update

about 18x per second.



Regards,

Peter T





-

Re:How to write out current time including milliseconds?

"Peter T" <peter_t@discussions>wrote in message

Quote
"Mike Williams" <mikea@whiskyandCoke.com>wrote in message

>"Peter T" <peter_t@discussions>wrote in message

>

>>Curiosity, is the API ever more accurate than simply 'Timer'.

>

>If you mean accuracy in terms of the actual real world time then none of

the

>methods is particularly accurate, and certainly not to within a few

>milliseconds, even if your machine is regularly updated from a reliable

time

>source.



I guess what I really meant to ask was - is using the GetLocalTime API any

more accurate than 'Timer' to return "current time" or fractional seconds

since midnight. In a light test they both seem about the same, ie update

about 18x per second.



Regards,

Peter T



fwiw, if display is all you need, there are many options. If you plan on

timing sections of your app, though (like do something, wait 5 seconds, do

something else)... better stay away from Timer, since it rolls over at

midnight.





--

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

In Loving Memory - www.vbsight.com/Remembrance.htm">www.vbsight.com/Remembrance.htm





-

Re:How to write out current time including milliseconds?

"Peter T" <peter_t@discussions>wrote in message



Quote
In a light test they both seem about the same, ie

update about 18x per second.



TimeGetTime gives you a resolution of 1 millisecond, provided you use

timeBeginPeriod 1 before you start using it, and it returns the number of

milliseconds since Windows was last started. You could use that in

conjunction with he system time to give you your desired resolution.



Mike





-