在IF语句中使用当前时间

时间:2013-07-12 09:14:34

标签: vb.net

我有以下代码

        Dim time As Date
    Dim currentHour As Integer
    Dim currentMinute As Integer

    time = DateTime.Now
    currentHour = time.Hour
    currentMinute = time.Minute

    If currentHour > 10 AndAlso currentMinute > 8 Then
        Response.Redirect("holdingpage.aspx")

    Else
        'do nothing
    End If

Bicallyically我想重定向到另一个页面,如果当前时间是在10.08am之后但没有任何反应。

2 个答案:

答案 0 :(得分:1)

好吧,如果是11:00,你的代码就会失败。

使用

If currentHour > 10 OrElse (currentHour = 10 AndAlso currentMinute > 8) Then

甚至更好

If Date.Now.TimeOfDay > New TimeSpan(10, 8, 0) Then

答案 1 :(得分:-1)

对它进行排序,我基本上将其添加到了英国的10.30,所以它失败了。

代码现在

        Dim time As Date
    Dim currentHour As Integer
    Dim currentMinute As Integer

    time = DateTime.Now
    currentHour = time.Hour
    currentMinute = time.Minute

    If currentHour >= 10 And currentMinute >= 8 Then
        Response.Redirect("HoldingPage.aspx")

    Else
        'do nothing
    End If