57

I am searching how to format time including microseconds. I'm using class DateTime, it allowes (using properties) to get data till miliseconds, which is not enougth. I tried using Ticks, but I didn't know how to translate it to microseconds.

Boris Raznikov
  • 2,373
  • 10
  • 34
  • 56
  • 5
    Be careful with DateTime.Now, it is not accurate to the microsecond. Try System.Diagnostics.Stopwatch to grab the time. Also make sure you have a high-resolution system clock. – Rob Elliott Jul 30 '09 at 16:27
  • Yes, You are right. It took my time to understand this comment. Although It presents the time in microsceonds, the time is not in microseconds. – Boris Raznikov Apr 21 '10 at 07:11

4 Answers4

104

You can use "ffffff" in a format string to represent microseconds:

Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffffff"));

To convert a number of ticks to microseconds, just use:

long microseconds = ticks / (TimeSpan.TicksPerMillisecond / 1000);

If these don't help you, please provide more information about exactly what you're trying to do.

EDIT: I originally multiplied ticks by 1000 to avoid losing accuracy when dividing TimeSpan.TicksPerMillisecond by 1000. However, It turns out that the TicksPerMillisecond is actually a constant value of 10,000 - so you can divide by 1000 with no problem, and in fact we could just use:

const long TicksPerMicrosecond = 10;

...

long microseconds = ticks / TicksPerMicrosecond;
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I used both of the opyions the problem is that I get the microseconds always zero. It seems like it can't get the resolution of that. – Boris Raznikov Jul 30 '09 at 13:13
  • 2
    Then that's not a formatting problem, it's a resolution problem. Where are you getting the data from to start with? If you're trying to time how long something takes, you should use the `Stopwatch` class. – Jon Skeet Jul 30 '09 at 13:17
  • 1
    No. I just need it for the logger. For every event in the system I enter at the beginning the time it happened. Any suggetsions ? – Boris Raznikov Jul 30 '09 at 14:24
  • You could try fetching the accurate system time using P/Invoke, but I don't know how accurate you'll find it to be... and it may be a bit of a pain. – Jon Skeet Jul 30 '09 at 14:36
  • I tried it another windows (mine is vista) and it resolution f the micrseconds – Boris Raznikov Jul 31 '09 at 12:05
  • I think better approach is to use `Stopwatch` class. When using date time ticks it offten returns 0 if function is really fast. – Mr. Blond Feb 28 '16 at 18:09
  • 3
    @EdgarsŠturms: Stopwatch is the right answer for measuring *elapsed* times - but it's not appropriate to obtain the "time of day". – Jon Skeet Feb 28 '16 at 19:47
20

I was unable to get Johns tick to micorosecond conversion to work. Here is how I was able to measure Microsecond and Nanosecond resolution by using ticks and the Stopwatch:

Stopwatch sw = new Stopwatch();
sw.Start();

// Do something you want to time

sw.Stop();

long microseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L*1000L));
long nanoseconds = sw.ElapsedTicks / (Stopwatch.Frequency / (1000L*1000L*1000L));

Console.WriteLine("Operation completed in: " + microseconds + " (us)");
Console.WriteLine("Operation completed in: " + nanoseconds + " (ns)");
  • sw.Elapsed.TotalMilliseconds is nice. Then divide by 1000 to get microseconds. – rocketsarefast Nov 14 '16 at 18:23
  • 1
    This is not quite right, because (Stopwatch.Frequency / (1000L*1000L)) being a long value leaves everything after a decimal for example if the result of the equation in the line above (of my comment) is 2.8576 which is technically 3 but sw.ElapsedTicks will be divided by 2 not 2.8 or 3, which is totally wrong. – user2913184 Feb 20 '18 at 20:01
10

"ffffff" is what you need.

return DateTime.Now.ToString("HH:mm:ss.ffffff");
Josip Medved
  • 3,631
  • 1
  • 28
  • 36
3

It's:

return DateTime.Now.ToString("HH:mm:ss.ffffff");

for the whole time stamp in format hours:minutes:seconds:microseconds

return DateTime.Now.ToString(".ffffff");

if you need only residual microseconds.