Future Date Woes

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

I have a simple application that handles authentication, and one of the
things it checks is password aging. If I have something like this:

If ("02/14/2007" <= Date.Today.ToString("MM/dd/yyyy")) Then
Messagebox.Show("Date specified is less than current")
Else
Messagebox.Show("Date specified is greater than current")
End If


This is causing problems with authentication, as the future date, when
it rolls into another calander year, this expression consistently
returns that the date specified is less than the current date, even if
the date is 01/01/2007 and the current date is 12/31/2006. I haven't
yet found a way around this short of iterating through each number in
the date starting with the year.. Any suggestions?
 
Try converting it to a real date, or reverse the order
of the date format so it's 2007/02/14.

Robin S.
 
Your prime issue is that the test you are performing is "02/14/2007" <=
"12/31/2006".

The first character of the string on the left is logically less than the the
first character of the string on the right ("0" < "1") therefore the result
is true.

If you just deal with the values as dates then you will be fine:

Dim _d As New DateTime(2007, 2, 14)

If _d <= Datetime.Today Then
...

I also thingk that you have a logic issue with you test. You are testing for
<= (less than or equal to). This will give a result of true when the 2 dates
are the same and therefore your message is inappropriate. I think your test
should simply be for < (less than).
 
Thanks! That worked.


Stephany said:
Your prime issue is that the test you are performing is "02/14/2007" <=
"12/31/2006".

The first character of the string on the left is logically less than the the
first character of the string on the right ("0" < "1") therefore the result
is true.

If you just deal with the values as dates then you will be fine:

Dim _d As New DateTime(2007, 2, 14)

If _d <= Datetime.Today Then
...

I also thingk that you have a logic issue with you test. You are testing for
<= (less than or equal to). This will give a result of true when the 2 dates
are the same and therefore your message is inappropriate. I think your test
should simply be for < (less than).
 
Kevin,

The most made error is that if people want to compare dates they still take
the date and time.

To compare Dates you can use on both sides DateTime.Date

I hope this gives an idea,

Cor
 
Kevin said:
I have a simple application that handles authentication, and one of the
things it checks is password aging. If I have something like this:

If ("02/14/2007" <= Date.Today.ToString("MM/dd/yyyy")) Then
Messagebox.Show("Date specified is less than current")
Else
Messagebox.Show("Date specified is greater than current")
End If


This is causing problems with authentication, as the future date, when
it rolls into another calander year, this expression consistently
returns that the date specified is less than the current date, even if
the date is 01/01/2007 and the current date is 12/31/2006. I haven't
yet found a way around this short of iterating through each number in
the date starting with the year.. Any suggestions?

Try enclosing the literal date in Pound-signs ie: if ( #02/14/2007# <= ...
otherwise you're comparing a string to a date.
HTH
 
Back
Top