Time Comparison - why is this not working?

J

Joey Martin

Please look at code below. I am trying to compare the NOW time with a
"deadline" time. Please help. Just not sure why this is not working. I
need to be able to say IF IT'S BEFORE 9:30 TODAY, IT'S OKAY TO ADD
SOMETHING. IF IT'S AFTER 9:30 TODAY, YOU MUST ADD IT TOMORROW.

CODE:
nowtime=now()
deadlinetime=formatdatetime(now(),2) + " 8:30:00 AM"
response.write "NOW: " & nowtime & "<BR>"
response.write "Deadline: " & deadlinetime & "<BR>"
if nowtime<deadlinetime then response.write "can send out today" end if
if nowtime>deadlinetime then response.write "must send out tomorrow" end
if

RESULTS:
NOW: 1/18/2007 8:51:43 AM
Deadline: 1/18/2007 8:30:00 AM
can send out today

As you can see, NOW is GREATER THAN Deadline, so it should must send out
tomorrow.
 
E

Evertjan.

Joey Martin wrote on 18 jan 2007 in
microsoft.public.inetserver.asp.general:
Please look at code below. I am trying to compare the NOW time with a
"deadline" time. Please help. Just not sure why this is not working. I
need to be able to say IF IT'S BEFORE 9:30 TODAY, IT'S OKAY TO ADD
SOMETHING. IF IT'S AFTER 9:30 TODAY, YOU MUST ADD IT TOMORROW.

CODE:
nowtime=now()
deadlinetime=formatdatetime(now(),2) + " 8:30:00 AM"
response.write "NOW: " & nowtime & "<BR>"
response.write "Deadline: " & deadlinetime & "<BR>"
if nowtime<deadlinetime then response.write "can send out today" end if
if nowtime>deadlinetime then response.write "must send out tomorrow" end

Those variables will be compared as strings, letter by letter!
Make datevalues!

Try:

nowtime = time()
deadlinetime = #08:30:00#

response.write "Now: " & nowtime & "<BR>"

response.write "Deadline: " & deadlinetime & "<BR>"

if nowtime < deadlinetime then
response.write "can send out today"
else
response.write "must send out tomorrow"
end if
 
B

Bob Barrows [MVP]

Joey said:
Please look at code below. I am trying to compare the NOW time with a
"deadline" time. Please help. Just not sure why this is not working. I
need to be able to say IF IT'S BEFORE 9:30 TODAY, IT'S OKAY TO ADD
SOMETHING. IF IT'S AFTER 9:30 TODAY, YOU MUST ADD IT TOMORROW.

CODE:
nowtime=now()
deadlinetime=formatdatetime(now(),2) + " 8:30:00 AM"

formatdatetime returns a string. You are doing a string comparison
instead of a datetime comparison. Change this to:

deadlinetime = dateadd("n",30,dateadd("h",8,date()))

or, if you are committed to using formatdatetime, do this:

deadlinetime=formatdatetime(now(),2) + " 8:30:00 AM"
deadlinetime=CDate(deadlinetime)

Personally, I prefer the former
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top