格式化时间从秒到mm:ss:ms:微秒

时间:2016-12-28 12:16:04

标签: c# unity3d time timespan time-format

我有这个疑问,如果我有几秒钟的时间,我想代表它: " mm:ss:毫秒:微秒",这段代码是否正确?

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秒是1 * 10 ^ 6微秒,1秒是1000毫秒。是的,这段代码?微秒有6位数。

由于

1 个答案:

答案 0 :(得分:0)

您可以使用:

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;