Click to See Complete Forum and Search --> : Having a problem with an IF statement...
NoahC
11-18-2003, 10:47 AM
OK... first off, here's my code...
Dim holidays
holidays = Array("1/1/2003","4/18/2003","5/26/2003","7/4/2003","9/1/2003","11/27/2003","11/28/2003","12/24/2003","12/25/2003","12/31/2003")
For I = 0 to Ubound(holidays)
response.write("a<br>")
response.write(holidays(I) & "<br>")
response.write(date & "<br>")
if date() = holidays(I) then
response.write("spiffy!<br>")
end if
Next
Now, what I've done is set the server date to 12/25/2003, and as a result I expect it to, if the date is the same as the server date, put "spiffy!" after them... instead here is the output
a
1/1/2003
12/25/2003
a
4/18/2003
12/25/2003
a
5/26/2003
12/25/2003
a
7/4/2003
12/25/2003
a
9/1/2003
12/25/2003
a
11/27/2003
12/25/2003
a
11/28/2003
12/25/2003
a
12/24/2003
12/25/2003
a
12/25/2003
12/25/2003
a
12/31/2003
12/25/2003
As you can see, everything is working except that, when the date in the holidays array matches the server date, it doesn't go into the if statement and output "spiffy!"... any ideas?
gil davis
11-18-2003, 10:54 AM
date() is an object. holidays(i) is a string. They will never compare correctly.
NoahC
11-18-2003, 11:01 AM
Didn't strike me that it might be a variable type mismatch... anyway, this is the suggestion that someone else made on another forum, and it worked. Thanks all!
try this
code:--------------------------------------------------------------------------------
if(Date() = CDate(Holidays(I))) then
Response.write("Spiffy")
end if
--------------------------------------------------------------------------------
The problem may be you are trying to compare a Date to a String...so you should convert the String to a Date using CDate() and then compare them.
slyfox
11-18-2003, 11:07 AM
Dim holidays
holidays = Array("1/1/2003","4/18/2003","5/26/2003","7/4/2003","9/1/2003","11/27/2003","11/28/2003","12/24/2003","12/25/2003","12/31/2003")
For I = 0 to Ubound(holidays)
dim h, d
h = (holidays(I) & "<br>")
d = (date & "<br>")
response.write("a<br>")
response.write(h)
response.write(d)
if string(h) = string(d) then
response.write("spiffy!<br>")
end if
Next
slyfox
11-18-2003, 11:10 AM
i believe you get the "picture"..
hope it helps though... i don't normally do vbscript
NoahC
11-18-2003, 11:16 AM
Neither do I... at the bottom of this I've pasted the code that I'm currently using... the purpose of it is that, well... on our website we've setup a form so that it will always send an e-mail to someone (outside of this code)... but so that when it is a holiday, weekend, or during non-business hours it will send another e-mail to a 2nd email address on a mobile device. That way if someone tries to contact our company when we're not in the office, we will always get their e-mail (note that we leave early on Fridays).
Dim holidays
holidays = Array("1/1/2003","4/18/2003","5/26/2003","7/4/2003","9/1/2003","11/27/2003","11/28/2003","12/24/2003","12/25/2003","12/31/2003")
For I = 0 to Ubound(holidays)
if(Date() = CDate(Holidays(I))) then
'send email
end if
Next
if weekday(date) < 6 and weekday(date) > 0 and hour(now) < 18 and hour(now) > 7 then
'do nothing
elseif weekday(date) = 6 and hour(now) < 16 and hour(now) > 7 then
'do nothing
elseif weekday(date) = 6 and hour(now) = 16 and minute(now) < 16 then
'do nothing
else
Dim a
a = 0
For I = 0 to Ubound(holidays)
if(Date() = CDate(Holidays(I))) then
a = 1
end if
Next
if a = 0 then
'send email
end if
end if
CardboardHammer
11-24-2003, 01:49 AM
If the suggestions already made don't get you to the solution, I'd suggest debugging by doing a response.write of both dates. My guess is that one or both of them will be showing hours:minutes:seconds, which will screw things up... I encountered that sort of scenario when doing date comparisons under somewhat different (but still somewhat similar) circumstances.
EDIT: I'd be fairly willing to bet money that Date() isn't giving you a result in the format that you're defining your holidays in.
EDIT: Instead of converting a string to a date, convert the Date() into a string of the matching format.