如何在知道总步数、当前步数和持续时间(毫秒)的情况下以 hh:mm:ss 计算剩余时间?

发布于 2024-10-06 02:43:43 字数 449 浏览 0 评论 0原文

问题如标题,这不起作用,不明白为什么:

  // Get total steps, current step and duration in milliseconds
  int current = stats[0]; int total = stats[1]; int duration = stats[2];

  // Calculate the time span (of remaining time)
  var remaining = TimeSpan.FromMilliseconds((total - current) * duration);

  // Update the label
  label.Text = string.Format("Tempo rimanente: {0}",
    (new DateTime(remaining.Ticks)).ToString("hh:mm:ss"));

Question as title, this is not working, can't understand why:

  // Get total steps, current step and duration in milliseconds
  int current = stats[0]; int total = stats[1]; int duration = stats[2];

  // Calculate the time span (of remaining time)
  var remaining = TimeSpan.FromMilliseconds((total - current) * duration);

  // Update the label
  label.Text = string.Format("Tempo rimanente: {0}",
    (new DateTime(remaining.Ticks)).ToString("hh:mm:ss"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昨迟人 2024-10-13 02:43:43

尝试更改

(new DateTime(remaining.Ticks)).ToString("hh:mm:ss"));

remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds);

OR 甚至:

 // Update the label
 label.Text = string.Format("Tempo rimanente: {0:00}:{1:00}:{2:00}",
    remaining.Hours, remaining.Minutes, remaining.Seconds)

Try changing

(new DateTime(remaining.Ticks)).ToString("hh:mm:ss"));

To

remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds);

OR even:

 // Update the label
 label.Text = string.Format("Tempo rimanente: {0:00}:{1:00}:{2:00}",
    remaining.Hours, remaining.Minutes, remaining.Seconds)
樱娆 2024-10-13 02:43:43

您为什么要尝试将 TimeSpan 转换为 DateTime ? “剩余时间”这个概念非常适合 TimeSpan,而不是 DateTime。您可能希望将其转换为“预计完成时间”,即 DateTime,但否则只需使用 TimeSpan

请注意,在 .NET 4 中,TimeSpan 获得了自定义格式功能,如果你真的需要它们 - 但我怀疑默认格式可能适合你,至少在开始时是这样。

Why are you trying to convert a TimeSpan to a DateTime at all? "Remaining time" is a concept which is ideally suited to a TimeSpan, not a DateTime. You might want to convert it to the "estimated completion time" which would be a DateTime, but otherwise just use the TimeSpan.

Note that in .NET 4, TimeSpan gained custom format abilities, if you really need them - but I suspect the default format is likely to be okay for you, at least to start with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文