C# time in microseconds

c#

Solution

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;

Problem

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.

Original source