Link to home
Start Free TrialLog in
Avatar of hxia
hxia

asked on

Milliseconds

How do I get the current time in milliseconds in VB?
Avatar of glass_cookie
glass_cookie

Hi!

Do this:

Private Sub Form_Load()
Dim hour As Long
Dim min As Long
Dim sec As Long
Dim total As Long

hour = Format(Time, "HH")
hour = hour * 3600000

min = Format(Time, "MM")
min = min * 60000

sec = Format(Time, "SS")
sec = sec * 1000

total = hour + min + sec

Msgbox total

That's it!

glass cookie : )

End Sub
From the above code, total is the number of miliseconds from all the hours, minutes and seconds of the day.

That's it!

glass cookie : )
Avatar of hxia

ASKER

glass cookie,

Thanks, but that doesn't precise the curretn system time to milliseconds.  My program monitors data traffic, that could happen in 1/10 second, and I need to know the time differece between each one.  I guess my question wasn't so clear.  I need to know HH:MM:SS PLUS the real current time of millisenconds, I don't just need to calculate display the HHMMSS into millisenconds.

Thanks,
hxia


ASKER CERTIFIED SOLUTION
Avatar of jklmn
jklmn

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
Although the calculation above is correct, I did some research and found that the Time function in VB only seems to track seconds.

Here's me test:
* Add a listbox on a blank form, width about 3000
* Add this code:

Private Sub Form_Click()
  Dim i As Integer
  List1.Clear
  For i = 1 To 15000
  List1.AddItem CDbl(Time)
  Next i
End Sub

* Run and click on the form
-> Notice that value in the listbox, filled in from Time, only change once in a while.

I converted the numbers in the listbox and found that they change at one-second intervals!

So...
You can calculate milliseconds from Time, but you'll always get zero (actually .9999999, so I guess this would round up to the next second.)

To verify, take any number in the listbox, such as .460185185185 (11:02:39), and:
* multiply by 24 (hours per day, since time represents a fraction of a day.)  You get 11 (11th hour.)
* subtract the hour to get a new fraction, and multiply by 60 (minutes per hour.)  You get 2 (2nd minute.)
* subtract the minutes to get a new fraction, and multiply by 60 (seconds per minute.)  You get 39 (39th second.)
* subtract the seconds to get a new fraction, multiply by 100 (milliseconds per second.) You always seem to get 99.9999[other digits]
Avatar of hxia

ASKER

Thank you all.  jklmn's solution is perfect, he da man!

hxia
jklmn, what do the ticks represent?  Number of milliseconds since midnight?

It gives much better precision in my listbox, but I'm not sure how to interpret the numbers since they're not fraction of a day like the Time function is.
Hi rspahitz,

That is the time from Windows last started:-)

Can you times that vy 1000 and get seconds, divide the seconds by 60 and get minutes....