Comparing Two Dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike N.

    Comparing Two Dates

    I am currently using a function to validate a form on the client side
    (see code below). At the end of the function, I would like it to also
    compare a startDate against an endDate to ensure that the endDate is
    greater than (comes after) the startDate. The date format I'm using is
    MM/DD/YYYY and it's writing to an MS SQL Server 2000 database table
    via ASP. THANKS!!!

    function ValidateForm()
    {
    //validate STATUS element
    if (document.form. status[0].selected)
    {
    alert("Please select a Status");
    document.form.s tatus.focus();
    return false;
    }

    //validate NAME element
    if (document.form. name.value=="")
    {
    alert("Please enter a Name")
    document.form.n ame.select();
    return false;
    }

    //validate START DATE element
    if ((document.form .startDate.valu e=="") ||
    (document.form. startDate.value =="mm/dd/yyyy"))
    {
    alert("Please enter a valid Start Date")
    document.form.s tartDate.focus( );
    return false;
    }

    //validate END DATE element
    if ((document.form .endDate.value= ="") ||
    (document.form. endDate.value== "mm/dd/yyyy"))
    {
    alert("Please enter a valid End Date")
    document.form.e ndDate.focus();
    return false;
    }

    //validate DATES
    //check to ensure that endDate is > startDate
    }
  • Thomas 'PointedEars' Lahn

    #2
    Re: Comparing Two Dates

    Mike N. wrote:[color=blue]
    > I am currently using a function to validate a form on the client side
    > (see code below). At the end of the function, I would like it to also
    > compare a startDate against an endDate to ensure that the endDate is
    > greater than (comes after) the startDate. The date format I'm using is
    > MM/DD/YYYY [...][/color]

    The Date() constructor takes the year, month and date as arguments (in this
    order, among others) and creates Date objects which have a getTime() method
    to reflect the number of milliseconds that have passed for this date since
    January 1, 1970 00:00:00.

    RegExp.exec() used on the input string will let you extract year, month and
    date from it.

    This has been explained in detail several times before. RTFM, RTFFAQ.


    PointedEars

    Comment

    • Dr John Stockton

      #3
      Re: Comparing Two Dates

      JRS: In article <41070FCB.20906 06@PointedEars. de>, dated Wed, 28 Jul
      2004 04:30:35, seen in news:comp.lang. javascript, Thomas 'PointedEars'
      Lahn <PointedEars@nu rfuerspam.de> posted :[color=blue]
      >Mike N. wrote:[color=green]
      >> I am currently using a function to validate a form on the client side
      >> (see code below). At the end of the function, I would like it to also
      >> compare a startDate against an endDate to ensure that the endDate is
      >> greater than (comes after) the startDate. The date format I'm using is
      >> MM/DD/YYYY [...][/color]
      >
      >The Date() constructor takes the year, month and date as arguments (in this
      >order, among others) and creates Date objects which have a getTime() method
      >to reflect the number of milliseconds that have passed for this date since
      >January 1, 1970 00:00:00.[/color]

      Careless. You should be aware of the need to specify GMT or UTC here,
      even though it will not affect the OP's use.
      [color=blue]
      >RegExp.exec( ) used on the input string will let you extract year, month and
      >date from it.[/color]

      True, but not necessary.

      Browsers in locations where it is appropriate to specify MM/DD/YYYY will
      accept an MM/DD/YYYY string into new Date() .


      Dates in the ISO 8601 date field order can be compared directly, if the
      separators are constants, leading zeroes are used, the range 1000-9999
      is not exceeded, and four-digit years are used. That AIUI is a FIPS and
      ANSI standard, BTW.

      Dates in the OP's form can easily be converted to ISO, by concatenating
      substrings or using a RegExp; see below, and my datefmts.htm#RU SA, and
      UStoISO8601(St) in my js-date9.htm.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...