compare dates.

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

    #1

    compare dates.

    Hi,

    In a form I have the curent date
    <input name="datetoday " type="hidden" value="<? echo date("d/m/Y"); ?>">
    and
    <input type=text name="datebox" size=15>
    The date format is d/m/Y (day/month/year) in both case

    Now my problem is that i want to know if 'datebox' is valid, that means
    after 'datetoday'.

    I tried :

    function checkFields() {
    missinginfo = "";
    if (document.form. datebox.value < document.form.d atetoday.value) {
    missinginfo += "\n - date not valid";
    }

    But this doesn't work because of the '/'.

    Can someone help me, thanks,

    AL

  • Lasse Reichstein Nielsen

    #2
    Re: compare dates.

    alexis <test@test.co m> writes:
    [color=blue]
    > In a form I have the curent date[/color]
    [color=blue]
    > The date format is d/m/Y (day/month/year) in both case[/color]

    I'll assume you'll have checked this, so we can trust the format.
    You should consider that that date format is ambiguous, since people
    in some coutries will read it as m/d/Y if they can.
    [color=blue]
    > Now my problem is that i want to know if 'datebox' is valid, that
    > means after 'datetoday'.[/color]
    [color=blue]
    > I tried :[/color]
    [color=blue]
    > if (document.form. datebox.value < document.form.d atetoday.value) {[/color]

    You are comparing them as strings.
    [color=blue]
    > But this doesn't work because of the '/'.[/color]

    That's not the problem. If the format was YYYY/mm/dd, then string
    comparison would work, because later dates would compare larger as
    strings. It doesn't work for you, because you have the least
    significant part of the date first, so "10/01/1970" is larger than
    "09/12/2036", because "1" is larger than "0" and the rest is ignored.

    To use string comparison, you *must* use the same number of digits
    for each part every time, so the 3rd will be "/03", not "/3".
    [color=blue]
    > Can someone help me, thanks,[/color]

    Either change the format, or parse the string.

    Start with:
    ---
    var today = document.forms['form'].elements['datetoday'].value;
    var someday = document.forms['form'].elements['datebox'].value;
    var todayParts = today.split("/");
    var somedayParts = someday.split("/");
    ---
    That gives you arrays where, e.g., "31/11/2003" becomes
    ["31","11","2003 "].

    You can now compare yourself or use the Date constructor:

    ---
    if ( (Number(todayPa rts[2]) < Number(somedayP arts[2])) ||
    (Number(todayPa rts[2]) == Number(somedayP arts[2]) &&
    ( (Number(todayPa rts[1]) < Number(somedayP arts[1])) ||
    ( (Number(todayPa rts[1]) == Number(somedayP arts[1])) &&
    (Number(todayPa rts[0]) < Number(somedayP arts[0])) ))))
    {
    // today is before someday.
    }
    ---
    or
    --
    var todayDate = Date.UTC(Number (todayParts[2]), //year
    Number(todayPar ts[1])-1, //month
    Number(todayPar ts[0]) //date
    );
    var somedayDate = Date.UTC(Number (somedayParts[2]), //year
    Number(somedayP arts[1])-1, //month
    Number(somedayP arts[0]) //date
    );

    if ( todayDate < somedayDate) {
    // today < someday
    }
    ---

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Steve van Dongen

      #3
      Re: compare dates.

      On Sun, 30 Nov 2003 12:43:13 +0100, alexis <test@test.co m> wrote:
      [color=blue]
      >Hi,
      >
      >In a form I have the curent date
      ><input name="datetoday " type="hidden" value="<? echo date("d/m/Y"); ?>">
      >and
      ><input type=text name="datebox" size=15>
      >The date format is d/m/Y (day/month/year) in both case
      >
      >Now my problem is that i want to know if 'datebox' is valid, that means
      >after 'datetoday'.
      >
      >I tried :
      >
      >function checkFields() {
      >missinginfo = "";
      >if (document.form. datebox.value < document.form.d atetoday.value) {
      >missinginfo += "\n - date not valid";
      >}
      >
      >But this doesn't work because of the '/'.[/color]

      var d1 = document.form.d atebox.value;
      var d2 = document.form.d atetoday.value;
      if (Date.parse(d1) < Date.parse(d2))
      ...

      http://msdn.microsoft.com/library/de...jsmthparse.asp

      You don't need to verify that the string entered by the user is a
      valid date format before attempting to perform the comparison.

      Regards,
      Steve

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: compare dates.

        Steve van Dongen <stevevd@hotmai l.com> writes:
        [color=blue][color=green]
        >>The date format is d/m/Y (day/month/year) in both case[/color][/color]
        [color=blue]
        > var d1 = document.form.d atebox.value;
        > var d2 = document.form.d atetoday.value;
        > if (Date.parse(d1) < Date.parse(d2))[/color]

        My browser's Date.parse parses "09/10/2003" as month/day/year, so it
        won't work.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Dr John Stockton

          #5
          Re: compare dates.

          JRS: In article <he0mjbf6.fsf@h otpop.com>, seen in
          news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
          posted at Sun, 30 Nov 2003 13:19:57 :-[color=blue]
          >alexis <test@test.co m> writes:
          >[color=green]
          >> In a form I have the curent date[/color]
          >[color=green]
          >> The date format is d/m/Y (day/month/year) in both case[/color][/color]

          That, of course, is not in accordance with an applicable European Norm.
          Brussels is not always wrong.

          [color=blue]
          >I'll assume you'll have checked this, so we can trust the format.
          >You should consider that that date format is ambiguous, since people
          >in some coutries will read it as m/d/Y if they can.[/color]

          And, as you have subsequently shown, browsers in more countries will.

          [color=blue]
          >Either change the format, or parse the string.[/color]

          S = '23/04/03'
          S = S.replace(/(\d+).(\d+).(\d +)/, '$3/$2/$1')
          S = S.replace(/^(\d\d\/)/, '20$1')
          D = new Date(S)

          and compare Date Objects. Assumes D M Y, and D is d or dd, M is m or
          mm, Y is yy or yyyy.

          S = S.replace(/\b(\d)\b/g, '0$1') // inserts leading zeroes

          and with leading zero and corrected (to ISO) order & yyyy one can
          compare strings.

          OP : Test carefully. Better to get dates in proper yyyy-mm-dd form in
          the first instance.


          Mote that if the date is first split into Y M D strings one can generate
          a PseudoDate = (Y*100+ +M)*100+ +D and compare those directly; the
          extra + signs and spaces are *vital*. H'mmm - +D+(+M+Y*100)*1 00 seems
          OK too, & +D+100*(+M+100* Y) . PseudoDates can be compared but should
          not be subtracted, except to obtain the sign.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
          Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
          PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
          Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

          Comment

          Working...