1

By using C# built-in API

System.DateTime.Now.Millisecond

I can generate milliseconds.

But When I think about

I could not find any built-in functions from

System.DateTime

Could anyone give me suggestion please?

Frank Myat Thu
  • 4,448
  • 9
  • 67
  • 113
  • Divide the milliseconds by the necessary amount? Erm, make that multiply. You'll never get the proper precision though. E.g. microseconds will always be multiples of 1000. Useless really. – Jason Down Jan 17 '12 at 05:06
  • Wow. You must really want accurate timestamps in your log files :-) – paxdiablo Jan 17 '12 at 05:08
  • `Ticks` will probably be the smallest amount of time you can get using native C# methods and even they will be less than reliable since your code doesn't run that quickly. Even ANSI C++ doesn't provide that granularity. – M.Babcock Jan 17 '12 at 05:09
  • 1
    What is this ANSI C++ of which you speak? ISO took over the standards many moons ago and ANSI, like other national standards bodies, simply rubber-stamps the ISO documents nowadays :-) – paxdiablo Jan 17 '12 at 05:18
  • Consider it a relic from the days that I actually felt like I was coding something rather than just reusing something someone at Microsoft coded for me. ;) – M.Babcock Jan 17 '12 at 05:23

6 Answers6

3

Given that the resolution of the date/time is only ten milliseconds, it's probably considered unnecessary to try and have properties containing anything with a smaller resolution than one millisecond.

The values for them would simply be the milliseconds value with some zeros tacked on the end.

Computers have come a long way since mid last century but, given that a photon running at the speed limit of the universe(a) will only cross a few hydrogen atoms in one attosecond, it's a stretch trying to imagine how useful such a resolution would be.

A (classic) CPU running at 5GHz would only get through five billionths of an instruction in that timeframe.


(a) One of my favorite quotes: The speed of light. It's not just a good idea, it's the law.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

For high resolution timing, you could look into the QueryPerformanceCounter function. I think it's also exposed via the Stopwatch class in C#. Of course, this is only a timer, not a walltime, so you'll have to combine it with DateTime to get actual timestamps.

SO1416139 has details on how to do that.

Community
  • 1
  • 1
tzaman
  • 46,925
  • 11
  • 90
  • 115
0

The smallest time unit on the Windows platform is the millisecond. You are not going to get any more accurate than that.

Keagan Ladds
  • 458
  • 3
  • 15
0

The smallest unit of time is the tick, which is equal to 100 nanoseconds. A tick can be negative or positive.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
Dialock
  • 263
  • 5
  • 11
  • [link](http://msdn.microsoft.com/en-us/library/system.timespan.ticks%28v=vs.95%29.aspx) Is the link for a actual example. My programming knowledge isn't developed enough to just spout of code. – Dialock Jan 17 '12 at 05:12
0

The most accurate you can get are ticks, which are 100 nano second amounts (0.1 Microseconds). You can access this via the Ticks property of a DateTime field, which is the number of ticks since 1 Jan 0001

Dermot
  • 1,713
  • 2
  • 18
  • 29
0

There is no direct methods available to get this..need to write our own method to get this...see this url 1) http://www.dotnetperls.com/convert-nanoseconds 2) C# time in microseconds

Thanks, Snoby

Community
  • 1
  • 1
snober
  • 229
  • 1
  • 3
  • 10