我无法理解为什么这个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:30
更改为1-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
应该返回&#39;未过期&#39;
任何能够提供帮助的人......我无法工作吗?
答案 0 :(得分:6)
"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.
答案 1 :(得分:1)
这些date constants也会按预期执行,并且在程序运行时不受区域设置的约束:
#3/4/2013 10:54:22# > #3/15/2013 12:23:30#
请记住,您需要使用美国日期格式作为常量。
答案 2 :(得分: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
答案 3 :(得分:-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