Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Compare Dates in ASP

    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 <font color=magenta>dd/mm/yyyy.</font color=magenta>. 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 <font color=magenta>dd/mm/yyyy.</font color=magenta> 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?

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compare Dates in ASP

    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 <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Compare Dates in ASP

    <P ID="edit" class=small>(Edited by jscher2000 on 23-Apr-03 19:38. I think I had that backwards before.)</P>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.

  4. #4
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compare Dates in ASP

    thanks. CDate did the trick!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •