• Compare Dates in ASP

    • This topic has 3 replies, 3 voices, and was last updated 22 years ago.
    Author
    Topic
    #386474

    this might be easy, maybe not:

    I have a form (well, several) with a date range (drop-downs for month, day, year) and in the .asp based page used to preview the form, i assemble the three drop-down Request.Form values to create a string that looks just like a date in format dd/mm/yyyy.. Obviously it would be good to validate the data from the form before processing to make sure people don’t give a higher date range in the FROM than in the TO date fields (which are drop-down boxes on the .html form). Does ASP recognize a pattern like dd/mm/yyyy. as anything in particular, such that I can test for one date being more recent than the other? or do I need to do some Javascript validation first? — say, in the .html page?

    Viewing 1 reply thread
    Author
    Replies
    • #670845

      I think you’re looking for the CDate() function

      -Build a string from the individual values like so: strStartDate = Request.Form(“startDay”) & “/” & Request.Form(“startMonth”) & “/” & Request.Form(“startYear”)
      (Do the same thing for your End date)
      -Convert the date string into a date value: dteStartDate = CDate(strStartDate)
      (Do the same thing with the End date string)
      -Compare as necessary: If dteStartDate > dteEndDate Then…

      Hope this helps – post back with any further questions smile

    • #670863

      (Edited by jscher2000 on 23-Apr-03 19:38. I think I had that backwards before.)

      I think it might be a better user experience to validate prior to submission, but I can’t say I know the best syntax for that. A peek at my JavaScript book suggests something like this in your onSubmit event handler for the form:

      If (Date.parse(TO) < Date.parse(FROM))
      {
      alert("Time travel is not permitted. Try again.");
      return (false);
      }

      Date.parse(date-string) returns a value in milliseconds from 1 Jan 1970 00:00 GMT, so for all practical applications, you should always get a positive number. I don't know whether you need to feed it an actual time, or whether it will simply assume midnight in the absence of that information.

    Viewing 1 reply thread
    Reply To: Reply #671039 in Compare Dates in ASP

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel