将 TimeSpan 格式化为倒计时器的正确方法是什么?

发布于 2024-09-13 05:31:17 字数 792 浏览 9 评论 0原文

我有一个在网站上运行的计时器。它将检索用户订单到期之前的时间跨度。在大多数情况下,这工作得很好,服务器将返回初始剩余时间,并且 JavaScript 将进行倒计时。所以它显示

2:30 2:29 2:28

然后由于某种原因,在某些页面加载时(似乎发生在计时器剩余时间不足 60 秒),格式变为

-1:0-45 -1:0-46 -1:0-47 code>

这是负责格式化时间跨度的代码:

<%= (TimeRemaining.TotalMinutes - 1).ToString("N0") %>:<%= TimeRemaining.Seconds.ToString("N0").PadLeft(2,'0') %>

我也刚刚尝试了以下方法,得到了相同的结果。

<%= String.Format("{0:0}:{1:00}", TimeRemaining.TotalMinutes-1, TimeRemaining.Seconds)%>

我还检查了 TimeRemaining,如果 TotalSeconds <= 0,那么它只返回一个 new TimeSpan(0),因此它永远不应该为负数。这不是 javascript 搞砸了倒计时,因为我可以禁用它,但仍然会看到混乱的格式化时间。

有没有更好/更干净的方法来做到这一点?

I have a timer that runs on the website. It will retrieve a timespan of how long a user has until their order expires. For the most part this works fine, the server will return the initial time remaining and the javascript will do the countdown. So it displays

2:30 2:29 2:28

Then for some reason, on some of the page loads (seems to happen when the timer has less than 60 seconds remaining), the formatting turns to

-1:0-45 -1:0-46 -1:0-47

This is the code responsible for formatting the timespan:

<%= (TimeRemaining.TotalMinutes - 1).ToString("N0") %>:<%= TimeRemaining.Seconds.ToString("N0").PadLeft(2,'0') %>

I've also just tried the following with the same result.

<%= String.Format("{0:0}:{1:00}", TimeRemaining.TotalMinutes-1, TimeRemaining.Seconds)%>

I also have a check on the TimeRemaining, if the TotalSeconds is <= 0, then it just returns a new TimeSpan(0), so it should never be going negative. It's not the javascript thats screwing up the countdown, because I can disable it and still see the messed up formatted time.

Is there a better/cleaner way to do this?

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

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

发布评论

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

评论(1

染柒℉ 2024-09-20 05:31:17

我看到的一个错误是,您不应该在这里减去一:

TimeRemaining.TotalMinutes - 1

如果剩余时间少于三十秒,并且您减去一分钟,您将得到一个负数。

TimeSpan timeRemaining = TimeSpan.FromSeconds(25);
string s = (timeRemaining.TotalMinutes - 1).ToString("N0");
Console.WriteLine(s);

结果:

-1

相反,您应该将此数字向下舍入为整数(也称为截断)。您可以使用 Math.Floor 来完成此操作,但这也是可能的只需将数字转换为整数即可达到相同的效果:

(int)TimeRemaining.TotalMinutes

至于秒,我不知道为什么会出错。我认为这是因为你的剩余时间实际上可能会变成负值。该错误似乎不在您发布的代码中。

One error I can see is that you shouldn't be subtracting one here:

TimeRemaining.TotalMinutes - 1

If there is less than thirty seconds remaining and you subtract one minute you will get a negative number.

TimeSpan timeRemaining = TimeSpan.FromSeconds(25);
string s = (timeRemaining.TotalMinutes - 1).ToString("N0");
Console.WriteLine(s);

Result:

-1

Instead you should round this number down to an integer (also called truncation). You can do this with Math.Floor, but it is also possible to just cast the number to an integer to achieve the same effect:

(int)TimeRemaining.TotalMinutes

As for the seconds, I don't know why that is going wrong. I assume it is because your time remaining can in fact go negative. The error seems to not be in the code you posted.

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