Daniel Peñalba Daniel Peñalba - 1 year ago 117
C# Question

Convert milliseconds to human readable time lapse

I would like to format some commands execution times in a human readable format, for example:

3 -> 3ms
1100 -> 1s 100ms
62000 -> 1m 2s
etc ..

Taking into account days, hours, minutes, seconds, ...

Is it possible using

Answer Source

You can use TimeSpan class, something like this:

TimeSpan t = TimeSpan.FromMilliseconds(ms);
string answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", 

It's quite similar as this thread I've just found:

C#: What is the best way to convert seconds into (Hour:Minutes:Seconds:Milliseconds) time?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download