Need help comparing dates

  • Thread starter Thread starter Duppypog
  • Start date Start date
D

Duppypog

I'm trying to compare a date stored in a database with today's date using an
If statement, but it's not returning true. Example, value in database is
11/5/2003 with today being 11/6/2003. Can someone spot the problem?

Thanks,
Lynnette

Here's the code:
sSQL = "Select PWExpire FROM tblUsers where strUserName = '" & stUser & "'
AND strPassword = '" & hshPW & "'"


cmd = New OleDbCommand(sSQL, cnn)

Dim pwEx As DateTime = cmd.ExecuteScalar


If pwEx.ToString("d") > DateTime.Today Then
 
Duppypog said:
I'm trying to compare a date stored in a database with today's date using an
If statement, but it's not returning true. Example, value in database is
11/5/2003 with today being 11/6/2003. Can someone spot the problem?

Thanks,
Lynnette

Here's the code:
sSQL = "Select PWExpire FROM tblUsers where strUserName = '" & stUser & "'
AND strPassword = '" & hshPW & "'"


cmd = New OleDbCommand(sSQL, cnn)

Dim pwEx As DateTime = cmd.ExecuteScalar


If pwEx.ToString("d") > DateTime.Today Then

Part of the problem may be the string conversion you are doing. At that
point, and without strong typing, it may be comparing string lengths.

The problem with dates and times is that they are just that - not just
dates, but also times. I would try something like this, if you are trying to
do a date comparison:

Dim FirstDate as DateTime
FirstDate = DateSerial(pwEx.Year, pwEx.Month, pwEx.Day)
TodayDate = DateSerial(Now.Year, Now.Month, Now.Day)
'this will give both a time of 12:00 AM, so only dates are compared
if pwEx > TodayDate then
'do stuff
End if
 
THANK YOU!!


Richard K Bethell said:
using

Part of the problem may be the string conversion you are doing. At that
point, and without strong typing, it may be comparing string lengths.

The problem with dates and times is that they are just that - not just
dates, but also times. I would try something like this, if you are trying to
do a date comparison:

Dim FirstDate as DateTime
FirstDate = DateSerial(pwEx.Year, pwEx.Month, pwEx.Day)
TodayDate = DateSerial(Now.Year, Now.Month, Now.Day)
'this will give both a time of 12:00 AM, so only dates are compared
if pwEx > TodayDate then
'do stuff
End if
 
Back
Top