如何在知道总步数、当前步数和持续时间(毫秒)的情况下以 hh:mm:ss 计算剩余时间?
问题如标题,这不起作用,不明白为什么:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改
为
OR 甚至:
Try changing
To
OR even:
您为什么要尝试将
TimeSpan
转换为DateTime
? “剩余时间”这个概念非常适合TimeSpan
,而不是DateTime
。您可能希望将其转换为“预计完成时间”,即DateTime
,但否则只需使用TimeSpan
。请注意,在 .NET 4 中,
TimeSpan
获得了自定义格式功能,如果你真的需要它们 - 但我怀疑默认格式可能适合你,至少在开始时是这样。Why are you trying to convert a
TimeSpan
to aDateTime
at all? "Remaining time" is a concept which is ideally suited to aTimeSpan
, not aDateTime
. You might want to convert it to the "estimated completion time" which would be aDateTime
, but otherwise just use theTimeSpan
.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.