Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
simple question: how do i calculate system time in milliseconds?
chris
Posted

Hi,

Have a look here:
http://stackoverflow.com/a/4016511[^]
It is a C# answer, the VB.NET version would be this:
VB
Dim milliseconds As Long = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Feb-14 16:09pm    
Worth noting: this is not an accurate way; please see my answer.
Besides, it's important not to mix time span with a point in time.
—SA
It depends on what do you want to get: a point in time, or a time span between some point. Apparently those are conceptually different and are represented by different types:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[^].

Usually, if you are talking about milliseconds or something like that, the time span is implied. Please see:
http://msdn.microsoft.com/en-us/library/system.timespan.milliseconds%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan.totalmilliseconds%28v=vs.110%29.aspx[^].

Precise value is given by Total* properties; for example, not Milliseconds (representing number of full milliseconds, poor accuracy), but TotalMilliseconds. The discrepancy will be especially dramatic on hours or minutes. :-)

You can directly measure the time span, with very high accuracy: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx[^].

You can obtain the actual accuracy during rumtime:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.frequency(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.ishighresolution(v=vs.110).aspx[^].

—SA
 
Share this answer
 
DateTime has a millisecond[^] property.
Thus, try DateTime.Now.Millisecond.
 
Share this answer
 
Comments
TnTinMn 4-Feb-14 14:05pm    
The Millisecond property only returns the millisecond component (0 to 999) of the DateTime value.
OP asked: how do i calculate system time in milliseconds?
Sergey Alexandrovich Kryukov 4-Feb-14 16:09pm    
Worth noting: this is not an accurate way; please see my answer.
Besides, it's important not to mix time span with a point in time.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900