By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,223 Members | 1,371 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,223 IT Pros & Developers. It's quick & easy.

Time Comparison - why is this not working?

P: n/a

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 ***
Jan 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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)
Jan 18 '07 #2

P: n/a
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.
Jan 18 '07 #3

P: n/a


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

*** Sent via Developersdex http://www.developersdex.com ***
Jan 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.