Need help comparing dates

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
 
R

Richard K Bethell

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
 
D

Duppypog

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
 

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

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top