How to get milliseconds ?

F

fniles

In a VB6 programs and VB.NET programs using "Imports VBDT =
Microsoft.VisualBasic.DateAndTime", we use the function Timer like so:
VB6
MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." & Right(Format(Timer,
"#0.00"), 2)

VB.NET
Imports VBDT = Microsoft.VisualBasic.DateAndTime
MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." &
Right(VBDT.Timer.ToString("#0.00"), 2)

An example of the result of MyTime:
38624.73

In the above example, does 73 mean 73 miliseconds ? But, 1 seconds has 1000
miliseconds, so that doesn't make sense.

If the above function doesn't represent miliseond, what is the best way to
get the milliseconds ?

Thank you
 
C

CY

In a VB6 programs and VB.NET programs using "Imports VBDT =
Microsoft.VisualBasic.DateAndTime", we use the function Timer like so:
VB6
MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." & Right(Format(Timer,
"#0.00"), 2)

VB.NET
Imports VBDT = Microsoft.VisualBasic.DateAndTime
MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." &
Right(VBDT.Timer.ToString("#0.00"), 2)

An example of the result of MyTime:
38624.73

In the above example, does 73 mean 73 miliseconds ? But, 1 seconds has 1000
miliseconds, so that doesn't make sense.

If the above function doesn't represent miliseond, what is the best way to
get the milliseconds ?

Thank you

Public Declare Function GetTickCount Lib "kernel32" () As Long
is your friend...

//CY
 
F

fniles

Thank you for your quick reply.

But GetTickCount retrieves the number of milliseconds that have elapsed
since the system was started.
How can I get the milliseconds of that minute ?
for ex: 11:24:01 and 93 milliseconds.

Also, what does the Timer function that I use below returns in the last 2
digits ?
An example of the result of MyTime:
38624.73
In the above example, does 73 mean 73 miliseconds ?

..
 
A

Armin Zingler

fniles said:
In a VB6 programs and VB.NET programs using "Imports VBDT =
Microsoft.VisualBasic.DateAndTime", we use the function Timer like so:
VB6
MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." & Right(Format(Timer,
"#0.00"), 2)

VB.NET
Imports VBDT = Microsoft.VisualBasic.DateAndTime
MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." &
Right(VBDT.Timer.ToString("#0.00"), 2)

An example of the result of MyTime:
38624.73

In the above example, does 73 mean 73 miliseconds ? But, 1 seconds has 1000
miliseconds, so that doesn't make sense.

It means 0.73 second, which is 730 milliseconds.
If the above function doesn't represent miliseond, what is the best way to
get the milliseconds ?

I don't know what the Format function does, but the documentation on the
DateTime.ToString method states that "f" is a placeholder for
fractions of a seconds, so

Dim MyTime As String
MyTime = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss.fff")

should work. Note that the small letter 'm' is used with the ToString function
instead of 'n'.

Be aware the the unit is milliseconds but the resoultion of the system time
is usually much lower.
 
J

Jeff Johnson

MyTime = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." &
Right(VBDT.Timer.ToString("#0.00"), 2)

An example of the result of MyTime:
38624.73

That's some SERIOUSLY broken code if the format string "dd-MMM-yyyy
HH:nn:ss" returns "38624."

Seriously.
 
C

Cor Ligthert[MVP]

There are many types of timers in Net while also the Time can be measured by
using the timespan class and the datetime but to with the classic VB
DateDiff.

However, I get the idea that you simply need the stopwatch for your problem.

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Otherwise, tell us why you need it, because telling that you had it in VB6
does not make much sense as we know how many les say types of clock classes
there are.

Be aware that on a multiuser multitasking OS what all windows OS's are the
measured time is never exact.

Cor
 
D

DickGrier

I'm not really sure what you really want. However, when I want to determine
(something like) elapsed time in mS, I use Now.TimeOfDay.TotalMilliseconds,
then do some arithmetic. There are other possibilities, such as using a
StopWatch.

Dick

--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.
 
F

fniles

I need to find out up to the milliseconds how long it takes for a process to
do.
At the beginning of the process, I write to a log file, and when it's done,
I write to the log file again.
 
C

Cor Ligthert[MVP]

Yea therefore is the stopwatch



fniles said:
I need to find out up to the milliseconds how long it takes for a process
to do.
At the beginning of the process, I write to a log file, and when it's
done, I write to the log file again.
 
F

fniles

Sorry,
38624 is the result from Right(VBDT.Timer.ToString("#0.00"), 2)
and not from the dd-MMMM-yyyy
 
M

mayayana

I did not see you used cross posting, will you be so kind in your replies
Did you mean I shouldn't cross posting between vb.net and vb6 groups ?

Exactly. They're 2 different things and have two
different groups. You referred to both VB6 code
and VB.Net code, but you're using one or the other.
(You can't mix and match them.) So you should be
posting in that group and limiting your question to
the code you're actually using.
 
C

CY

My thought was to get the time between two events, then its OK (not
great but OK), if you want the accurate, exact time on the millisecond/
micro/nano from the PC, how about checking on a NTP or a GPS app of
some kind, they have nice clocks. ticks are nice to time an delta, a
difference but not to get the real time, is your PC clock exact? to
the microsecond, millisecond or even second? if using W32time it might
be off a bit. I compared NTP to GPS and got 0.246 ms off.

//CY
 
G

Gregory A. Beamer

In the above example, does 73 mean 73 miliseconds ? But, 1 seconds has
1000 miliseconds, so that doesn't make sense.

If the above function doesn't represent miliseond, what is the best
way to get the milliseconds ?

It is actually centiseconds, or ten milliseconds. So 730 milliseconds is
correct. Standard base 10 operation.

You can get the millisecond from ticks, as well, but i tis trickier, as
you have to have a point of reference. For this reason, ticks are more
useful for time elapsed.

For example, you might have a process you are timing.

Module Module1

Sub Main()
Dim timer1 As DateTime = DateTime.Now

Dim i As Integer
Dim d As Double

'Loop to waste some time
For i = 1 To 1000000

d += i

Next

Dim timer2 As DateTime = DateTime.Now

Dim timeElapsedInTicks As Long = timer2.Ticks - timer1.Ticks
Dim timeElapsedInMilliseconds As Double = timeElapsedInTicks / 1000

Console.WriteLine("Ticks: {0}", timeElapsedInTicks)
Console.WriteLine("Millseconds: {0}", timeElapsedInMilliseconds)

Console.Read()
End Sub

End Module

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
F

fniles

Thank you very much for your help.


Gregory A. Beamer said:
It is actually centiseconds, or ten milliseconds. So 730 milliseconds is
correct. Standard base 10 operation.

You can get the millisecond from ticks, as well, but i tis trickier, as
you have to have a point of reference. For this reason, ticks are more
useful for time elapsed.

For example, you might have a process you are timing.

Module Module1

Sub Main()
Dim timer1 As DateTime = DateTime.Now

Dim i As Integer
Dim d As Double

'Loop to waste some time
For i = 1 To 1000000

d += i

Next

Dim timer2 As DateTime = DateTime.Now

Dim timeElapsedInTicks As Long = timer2.Ticks - timer1.Ticks
Dim timeElapsedInMilliseconds As Double = timeElapsedInTicks / 1000

Console.WriteLine("Ticks: {0}", timeElapsedInTicks)
Console.WriteLine("Millseconds: {0}", timeElapsedInMilliseconds)

Console.Read()
End Sub

End Module

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
J

Jeff Johnson

38624 is the result from Right(VBDT.Timer.ToString("#0.00"), 2)
and not from the dd-MMMM-yyyy

STILL pretty broken if the function requests TWO characters and you get back
FIVE!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top