For Programmers: Free Programming Magazines  


Home > Archive > ASP > January 2007 > Time Comparison - why is this not working?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Time Comparison - why is this not working?
Joey Martin

2007-01-18, 6:56 pm


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.

*** Sent via Developersdex http://www.developersdex.com ***
Evertjan.

2007-01-18, 6:56 pm

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Bob Barrows [MVP]

2007-01-18, 6:56 pm

Joey Martin wrote:
> 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
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


Joey Martin

2007-01-18, 6:56 pm



THANK YOU for the prompt responses. I can always count on you all!

*** Sent via Developersdex http://www.developersdex.com ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com