Format time from seconds to mm:ss:ms:microseconds

2.7k views Asked by At

I have this doubt, if i have a time in seconds, and I want to represent it in: "mm:ss:milliseconds:microseconds", is correct this code?

var minutes = timeCurrent / 60;
var seconds = timeCurrent % 60;
var milliseconds = timeCurrent * 1000;
var microseconds = timeCurrent * 1000000;
milliseconds = milliseconds % 1000;
microseconds = nanoseconds % 1000000;
currentTime.text = String.Format("{0:00}:{1:00}:{2:000}:{3:000000}",
            minutes, seconds, milliseconds, microseconds);

1 second is 1*10^6 microseconds, and 1 second is 1000 milliseconds. Is correct, this code? The microseconds have 6 digits.

Thanks

1

There are 1 answers

5
Mohit Vashistha On

You can use:

TimeSpan t = TimeSpan.FromSeconds( timeCurrent  );

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

or

    var totalMiliSeconds = t.TotalMilliseconds;

    var totaolMicroSeconds = 1000.0 * t.TotalMilliseconds;