7

我无法弄清楚为什么这个 VB.net 代码不起作用..

然后我要做的是if value1 > value2显示一个消息框说已过期,否则显示一个消息框说未过期。

If "4-3-13 10:54:22" > "15-3-13 12:23:30" Then
    MsgBox("Expired")
Else
    MsgBox("Not Expired")
End If

每次它说过期,即使知道它不应该。

当我将它从它更改15-3-13 12:23:301-3-13 12:23:30它仍然说过期。

如果我将代码更改为:

If "4-3-13 10:54:22" < "15-3-13 12:23:30" Then
    MsgBox("Not Expired")
Else
    MsgBox("Expired")
End If

它仍然返回错误。

我该如何做到这一点:

DATE1 = 4-3-13 10:54:22

DATE2 = 15-3-13 12:23:30


IF DATE1 > DATE2 THEN
   Expired
else
   Not Expired

应该返回“未过期”

任何人都可以帮助..我不能工作吗?

4

4 回答 4

9
"4-3-13 10:54:22" > "15-3-13 12:23:30" 
'This condition states that you are comaparring strings not date

为了得到你所期望的结果,这样做,

cdate("4-3-13 10:54:22") > cdate("15-3-13 12:23:30")
'Convert the strings into date and then compare it.

日期

于 2013-03-04T06:59:35.940 回答
2

这些日期常量也将按照您的预期执行,并且在程序运行时不受语言环境的影响:

#3/4/2013 10:54:22# > #3/15/2013 12:23:30#

请记住,您需要对常量使用美国日期格式。

于 2013-03-05T11:42:03.643 回答
1

试试这个:

dim date1 as DateTime = DateTime.ParseExact("4-3-13 10:54:22", "MM-dd-yy HH:mm:ss")

dim date2 as DateTime = DateTime.ParseExact("15-3-13 12:23:30", "MM-dd-yy HH:mm:ss")

if date1 > date2 then
MsgBox("Expired")
else
MsgBox("Not Expired")
end if
于 2017-01-26T07:29:47.947 回答
-1

代码如下:

If Today.Year - dExpiryDate.Value.Year = 0 Then
    If Today.Month - dExpiryDate.Value.Month < 0 Then
        LBExpiryDate.Text = "THE ID IS OK"
        LBExpiryDate.BackColor = Color.Green

    Else
        If Today.Month - dExpiryDate.Value.Month = 0 Then

            If Today.Day - dExpiryDate.Value.Day < 0 Then
                LBExpiryDate.Text = "THE ID IS OK"
                LBExpiryDate.BackColor = Color.Green
            Else
                LBExpiryDate.Text = "THE AGE IS EXPIRED "
                LBExpiryDate.BackColor = Color.Red
            End If
            Else
            LBExpiryDate.Text = "THE AGE IS EXPIRED "
            LBExpiryDate.BackColor = Color.Red

        End If
    End If
Else
    LBExpiryDate.Text = "THE AGE IS EXPIRED "
    LBExpiryDate.BackColor = Color.Red
End If
End If
于 2015-07-25T00:27:41.123 回答