vb.net 查找日期是否大于另一个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15195089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:43:03  来源:igfitidea点击:

Find if date is more than another date

vb.netdatedatediff

提问by Aaron

I can't work out why this VB.net code is not working..

我不明白为什么这个 VB.net 代码不起作用..

What I am trying to do is if value1 > value2then show a messagebox saying expired else show a messagebox saying not expired.

我想要做的是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

everytime it comes up saying expired even knowing it shouldn't.

每次出现时都说过期,即使知道它不应该过期。

When I change it from 15-3-13 12:23:30to 1-3-13 12:23:30it still say expired.

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

If I change my code to be:

如果我将代码更改为:

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

It still returns wrong.

它仍然返回错误。

How do I make it so that:

我如何做到这一点:

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

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


IF DATE1 > DATE2 THEN
   Expired
else
   Not Expired

Should return 'Not expired'

应该返回“未过期”

Anyone able to help.. I can't work it ?

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

回答by Rajaprabhu Aravindasamy

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

In order to get the result as you've expected, do like this,

为了得到您预期的结果,请这样做,

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

CDATE

CDATE

回答by Mark Hurd

These date constantswill also do as you expect, and won't be subject to the locale when the program is run:

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

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

Just remember you need to use US Date Format for the constants.

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

回答by Jomal

Try this:

尝试这个:

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

回答by faleh

The code is below:

代码如下:

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