【问题标题】:Format time from seconds to mm:ss:ms:microseconds格式化时间从秒到 mm:ss:ms:microseconds
【发布时间】:2017-05-12 17:35:06
【问题描述】:

我有这个疑问,如果我有以秒为单位的时间,我想用以下形式表示它: “mm:ss:milliseconds:microseconds”,这段代码正确吗?

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 位数字。

谢谢

【问题讨论】:

  • 你为什么不使用DateTime类?
  • 现在什么时候?

标签: c# unity3d time timespan time-format


【解决方案1】:

你可以使用:

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;

【讨论】:

  • 谢谢,但我需要微秒
  • timeCurrent 是一个 double 值,是 Unity 中 deltaTime 的总和。
  • timeCurrent 是总秒数还是滴答数?
  • timeCurrent 是总秒数
  • 很好的解决方案,微秒有 6 位,但是这个解决方案微秒只有 3 位,其他 3 位为零,还有另一种方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
  • 2020-10-16
  • 1970-01-01
相关资源
最近更新 更多