Future Date Woes

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?
 
R

RobinS

Try converting it to a real date, or reverse the order
of the date format so it's 2007/02/14.

Robin S.
 
S

Stephany Young

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).
 
K

Kevin

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).
 
C

Cor Ligthert [MVP]

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
 
H

Hal Rosser

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top