7

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

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

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:30 to 1-3-13 12:23:30 it still say expired.

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 ?

Kara
  • 6,115
  • 16
  • 50
  • 57
Aaron
  • 3,389
  • 12
  • 35
  • 48

4 Answers4

9
"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

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • 2
    Don't use cdate on literal strings unless you've explicitly set the culture. Otherwise it'll throw an exception or give the wrong result for people in different countries where their computer's date format is different. – user1318499 Mar 17 '17 at 02:13
2

These date constants will 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.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
1

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
arghtype
  • 4,376
  • 11
  • 45
  • 60
Jomal
  • 11
  • 1
-1

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
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
faleh
  • 1
  • This has many bugs and is needlessly complex. For example, it will say "expired" if the year is before the expiry year. You can simply compare the DateTime objects directly instead of breaking them up into day, month, year. – user1318499 Mar 17 '17 at 02:19