Link to home
Start Free TrialLog in
Avatar of charie
charie

asked on

problem of compare the value of the date system and the date we pick from list box

hi expert,

 I'm doing the cinema booking system. So, i want to check out if the user choose the date which is same with the date from the computer, it will write the message to show it can't booking on that day with the checking of the showtime of he choose. ok, my code is like this:
<form id="SeatingPlanForm" name="SeatingPlanForm" method="post">
<select name="bookingdate" size="1" class="text">
<option value="<%response.write(date())%>"><%response.write(date())%></option>
<option value="<%response.write(date + 1)%>"><%response.write(date + 1)%></option>
<option value="<%response.write(date + 2)%>"><%response.write(date + 2)%></option>
</select>  /* this is the list box that user will choose the date they want*/

then will pass this value of bookingdate to another page, example 2.asp
<%
bookingdate = trim(Request.form("bookingdate"))

real_time = time()

diff = DateDiff("n",  real_time, showtime)
 
if ( (diff < 60) and (session("bookingdate") = date()) ) then
    response.write "<br>Booking time is over!!!<br>"
end if
%>

  My problem is (session("bookingdate") = date()) always get false....means it can't check the situation if the date user choose is same with the computer date......what wrong with my code???????Thanks in advance cz this is quite urgent for me...thanks a lot...



Avatar of monosodiumg
monosodiumg

I think your problem  may just be that Session("bookingdate") is a string and not a date and the comparison fails becuase th inbuilt type conversion doesn't get it right. Your date may also include a time component (check this), which would prevent the comparison from matching.

In your code, you do not need to take booking_date from the Session since you seem to get it from the Request anyway so you could instead write:

if ( (diff < 60) and (bookingdate = date()) ) then

To avoid lots of posible problems with date:
-always make conversions between dates and strings explicit (test that you are doing this safely by changing the user's or server's date format to UK or UK)
-be sure that if you want a date only and no time that you get it from a function that returns a date with a zero time component (that's the difference between Date and Now).
-when converting between dates and strings (as you need to do to pass Dates via Request) be sure to pick a standard format (I recommend yyyymmdd becuase it allows comparison)

Parse the collected date with CDate( theVariable ).

Remember to user the err object to determine if there was a problem parsing and then you can use the built-in compartors from vbscript

---
On Error Resume Next

Dim theDate

theDate = Request("bookingdate")
theDate = CDate(theDate)

if err.Number <> 0 then
     'error parsing date, do whatever is necessary
end if

-rca
Avatar of sybe
From your code sample it is unclear how you generate session("bookingdate").

Request("bookingdate") is a String (all values from Request are a String).
I disagree with using "On Error Resume Next" in the post above. Mainly because using "On Error Resume Next" in a page is generally a bad idea, because it surpresses errors, and you never know what goes wrong. There is a good alternative: use the IsDate() function.

If IsDate (bookingdate) Then
    If CDate(bookingdate) = Date() Then

   End If
Else
    Response.write "no date picked"
End If
I agree with Sybe re use for error trapping.. Only use error trapping for truly unnexpected or unanticipable errors.
>>  Only use error trapping for truly unnexpected or unanticipable errors.

yes, and then use it in a function, because when On Error Resume Next is placed in a function, it only "counts" for the function, and the error trapping can be handled within the function. But most things can be trapped using Idate(), IsNumeric(), IsObject(), TypeName() etc methods. Almost the only use of "On Error Resume Next" is with handling XML. You can't know if a String can be converted to XML and there is no IsXML() function.

>Almost the only use of "On Error Resume Next" ...
Hmmmm.... . There's an argument for having at least one On Err on every single page at the top level to catch any freak errors like a file not being available, external widgets screwing up, a disk or network connection becoming unavailable, flooding in the Sahara, ...

But if  Sybe is referring to stuff like predictable 'errors' e.g. the original on err suggestion, which is in the same category as using error trapping to catch a division by zero (you should be checking the divisor instead) , then we're in agreement.

We are getting off-topic here, but it sounds like an interesting and important discussion. I created a new question for this:

https://www.experts-exchange.com/questions/20935596/discussion-avoinding-and-using-On-Error-Resume-Next.html

ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial