1

i want to get time of my system with accuracy of micro second in c# How can i get this? i can get normal time but it's not enough for me.

string ZamanShoru = DateTime.Now.ToString("H:mm:ss")
Dmitry
  • 13,797
  • 6
  • 32
  • 48
  • 2
    Your machine is not equipped with an atomic clock, the kind of device needed to know the absolute time with microsecond precision. Even if you had one, reading such a clock reliably is an unsolvable problem, whether your code got interrupted by the operating system is unknowable. So the hardware isn't there, it isn't useful. Only *incremental* time can be measured with that kind of accuracy. Readily available in C# with the Stopwatch class. – Hans Passant Sep 26 '14 at 18:07
  • You should consider why you need that kind of resolution... are you really concerned about the exact time to the millisecond, or are you attempting to display time values somewhere so you can trace your code or something? If you are trying to measure very small intervals, there's better ways to do it. System.Diagnostics.Stopwatch may be more useful. – Clever Neologism Sep 26 '14 at 18:12
  • @HansPassant - wholeheartedly agree. Make it an answer please? :) – Matt Johnson-Pint Sep 26 '14 at 18:13
  • It is going to be closed, Jon is very popular. – Hans Passant Sep 26 '14 at 18:15
  • 1
    The other question is *not* a dup. That one is about *formatting* a `DateTime` to microsecond precision - which is absolutely fine. This question asks about getting the system time with that accuracy - which is a different question. – Matt Johnson-Pint Sep 26 '14 at 18:15
  • [This question and answer](http://stackoverflow.com/q/2143140/634824) are more relevant to this post than the suggested dup. – Matt Johnson-Pint Sep 26 '14 at 18:23
  • @HansPassant, how do you know about his hardware? – ths Sep 26 '14 at 19:41

2 Answers2

0

According to the corresponding description of DateTime.Ticks in the MSDN you can use this property to get a resolution of one hundred of a millisecond.

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond, or 10 million ticks in a second.

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
0

Just use the "fff" Custom Format Specifier or any number of f you require.

string ZamanShoru = DateTime.Now.ToString("H:mm:ss.ffff")
Black Frog
  • 11,595
  • 1
  • 35
  • 66