倒数计时器需要一些舍入? VB / Asp.Net

时间:2016-09-22 14:23:22

标签: asp.net vb.net

我已经为我写了这篇文章。它归结为我们的下一次销售,但是分钟不会改变,直到第二次降到30以下。时间不会改变,直到分钟降到30以下我不久前发布了这个,但提供的解决方案超出了我的范围。我只是想知道是否有人知道如果可以调整当前代码的方法吗?

    Dim seconds As Integer = DateDiff(DateInterval.Second, Date.Now, BootSale.nextDOW(DayOfWeek.Sunday))
    Dim days, hours, minutes As Integer
    days = seconds / 86400
    hours = (seconds Mod 86400) / 3600
    minutes = (seconds Mod 3600) / 60
    lblTimer.Text = days & " Day(s) " & hours & " Hour(s) " & minutes & " Minute(s) " ' & seconds Mod 60 & " Seconds"

2 个答案:

答案 0 :(得分:1)

您应该使用正确的.NET函数来执行此操作。 TimeSpa n结构已具备您需要的功能。

    Dim difference As TimeSpan = BootSale.nextDOW(DayOfWeek.Sunday).Subtract(DateTime.Now)

    Dim message As String = String.Format("{0} Day(s) {1} Hour(s) {2} Minute(s)", difference.Days, difference.Hours, difference.Minutes)

    lblTimer.Text = message

答案 1 :(得分:0)

.Net正在自动舍入这些内容,因为您没有option strict on。您可以使用Math.Floor始终向下舍入。

Dim seconds As Integer = DateDiff(DateInterval.Second, Date.Now, BootSale.nextDOW(DayOfWeek.Sunday))
Dim days, hours, minutes As Integer
days = Math.Floor(seconds / 86400)
hours = Math.Floor((seconds Mod 86400) / 3600)
minutes = Math.Floor((seconds Mod 3600) / 60)
lblTimer.Text = days & " Day(s) " & hours & " Hour(s) " & minutes & " Minute(s) " ' & seconds Mod 60 & " Seconds"

我已将其重写为函数,因此您可以看到我的测试用例正在运行。

Public Function TestDate(d1 As DateTime, d2 As DateTime) As String
    Dim seconds As Integer = DateDiff(DateInterval.Second, d1, d2)
    Dim days, hours, minutes As Integer
    days = Math.Floor(seconds / 86400)
    hours = Math.Floor((seconds Mod 86400) / 3600)
    minutes = Math.Floor((seconds Mod 3600) / 60)
    Return days & " Day(s) " & hours & " Hour(s) " & minutes & " Minute(s) "
End Function

Assert.AreEqual("2 Day(s) 15 Hour(s) 45 Minute(s) ", TestDate(n, n.Add(New TimeSpan(2, 15, 45, 45))))
Assert.AreEqual("2 Day(s) 5 Hour(s) 5 Minute(s) ", TestDate(n, n.Add(New TimeSpan(2, 5, 5, 5))))