查找日期是否超过另一个日期

Aar*_*ron 5 vb.net datediff date

我无法弄清楚为什么这个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
Run Code Online (Sandbox Code Playgroud)

每次它出现说过期甚至知道它不应该.

当我从改变它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
Run Code Online (Sandbox Code Playgroud)

它仍然返回错误.

我如何做到这样:

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

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


IF DATE1 > DATE2 THEN
   Expired
else
   Not Expired
Run Code Online (Sandbox Code Playgroud)

应该返回"未过期"

任何人都能帮忙..我不能工作吗?

Raj*_*amy 7

"4-3-13 10:54:22" > "15-3-13 12:23:30" 
'This condition states that you are comaparring strings not date
Run Code Online (Sandbox Code Playgroud)

为了得到你想象的结果,这样做,

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

CDATE

  • 除非您已明确设置区域性,否则不要在文字字符串上使用 cdate。否则,对于计算机日期格式不同的不同国家/地区的人们,它会抛出异常或给出错误的结果。 (2认同)