469,632 Members | 1,750 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Need help comparing dates

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

Nov 17 '05 #1
2 1645
"Duppypog" <du******@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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
Nov 17 '05 #2

THANK YOU!!
"Richard K Bethell" <so*****@spammingisevil.bad> wrote in message
news:un**************@TK2MSFTNGP12.phx.gbl...
"Duppypog" <du******@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Philip Townsend | last post: by
2 posts views Thread by Manny Chohan | last post: by
12 posts views Thread by colincolehour | last post: by
2 posts views Thread by cyber100 | last post: by
4 posts views Thread by cheryl | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.