Which should be the correct date format for Date.parse ?

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

    Which should be the correct date format for Date.parse ?

    Hy everybody.

    I'm not a html writer, but a sysadmin who's trying to help a user able
    to compile an online form with IE but not with Mozilla (Moz1.6, Ns7.1,
    Firefox 0.8+) due to a javascript date check.

    Let's go straight to the point:

    <script language="JavaS cript">
    alert("Date: "+Date.parse("2 000-01-01"))
    // On IE and Mozilla: "Date: NaN"

    alert("Date: "+Date.parse("0 1-01-2000"))
    // On IE: "Date: 946681200000"
    // On Mozilla: "Date: NaN"

    alert("Date: "+Date.parse("0 1/01/2000"))
    alert("Date: "+Date.parse("2 000/01/01"))
    // On IE and Mozilla: "Date: 946681200000"
    </script>

    This happens on italian win2000 and winxp, italian IE6, italian Moz1.6,
    english Ns7.1, english Firefox 0.8.0+.

    Obviously the solution is the third example, and I've yet mailed it to
    the webmaster, who is using the second format, 01-01-2000; but is it
    correct that in both Mozilla and IE Date.parse doesn't accept the
    iso8601 format 2000-01-01 or maybe is something of which mozilla
    developers should be aware? Or maybe the examples above are an improper
    use of Date.parse?

    I read the paragraph 15.9.4.2 of ecmascript 262 pdf, but it's not so
    clear about possible date formats; or maybe it refers to something that
    I cannot understand.


    Thank you.
    Matteo
  • Evertjan.

    #2
    Re: Which should be the correct date format for Date.parse ?

    Matteo wrote on 15 jun 2004 in comp.lang.javas cript:[color=blue]
    > Obviously the solution is the third example, and I've yet mailed it to
    > the webmaster, who is using the second format, 01-01-2000;[/color]

    This format cannot be used, since it is regional settings dependent:

    01-07-2004 means 1st of juli in some pc's but 7th of january in others.

    Do not trust all Italian pc's to have been set "correctly" .

    Do not test on 01-01-2000, since that date will be "correct" in both
    circumstances.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Matteo

      #3
      Re: Which should be the correct date format for Date.parse ?

      Evertjan. ha scritto:[color=blue]
      > Matteo wrote on 15 jun 2004 in comp.lang.javas cript:[color=green]
      >>Obviously the solution is the third example, and I've yet mailed it to
      >>the webmaster, who is using the second format, 01-01-2000;[/color]
      > This format cannot be used, since it is regional settings dependent:
      >
      > 01-07-2004 means 1st of juli in some pc's but 7th of january in others.
      >
      > Do not trust all Italian pc's to have been set "correctly" .
      >
      > Do not test on 01-01-2000, since that date will be "correct" in both
      > circumstances.[/color]

      This is right; but I was wondering also why both IE and Mozilla accept
      AAAA/MM/DD and don't accept AAAA-MM-DD format, that is an iso standard;
      maybe Date.parse is intended to handle only long formats like '15 Jun
      2004 07:59:19 GMT', and supports other formats only for proprietary
      extension ?

      Anyway, mine is just an academic discussion about which date standard
      formats the browsers should and shouldn't accept for Date.parse;
      certainly the 01-01-2000 format is an ambiguous format and must not be
      used to calculate dates.

      Bye.
      Matteo

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Which should be the correct date format for Date.parse ?

        Matteo schrieb:[color=blue]
        > [...] I was wondering also why both IE and Mozilla accept AAAA/MM/DD[/color]

        Downwards compatibility.
        [color=blue]
        > and don't accept AAAA-MM-DD format, that is an iso standard;[/color]

        It is still the wrong format:

        <http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/date.html#11931 37>
        <http://msdn.microsoft. com/library/en-us/script56/html/js56jsobjdate.a sp?frame=true>
        <http://msdn.microsoft. com/library/en-us/script56/html/js56jsmthparse. asp?frame=true>
        [color=blue]
        > maybe Date.parse is intended to handle only long formats like '15 Jun
        > 2004 07:59:19 GMT',[/color]

        That may be the reason. AFAIS the ECMAScript 3 specification does
        not specify the date format that Date.parse() should accept, only
        ways how its argument can be interpreted.
        [color=blue]
        > and supports other formats only for proprietary extension ?[/color]

        Apparently.
        [color=blue]
        > Anyway, mine is just an academic discussion about which date standard
        > formats the browsers should and shouldn't accept for Date.parse; [...][/color]

        Since date strings are always ambiguous, one should use

        new Date (year, month [, date [, hours [, minutes [, seconds [, ms ] ]
        ] ] ] )

        as clearly specified in ECMAScript 3, section 15.9.3.1, instead.


        PointedEars

        Comment

        • Dr John Stockton

          #5
          Re: Which should be the correct date format for Date.parse ?

          JRS: In article <cana10$90j$1@b alena.cs.interb usiness.it>, seen in
          news:comp.lang. javascript, Matteo <matteoNo.luSpa mnardi@libNoero Spam.it>
          posted at Tue, 15 Jun 2004 19:01:47 :[color=blue]
          >Evertjan. ha scritto:[color=green]
          > > Matteo wrote on 15 jun 2004 in comp.lang.javas cript:[color=darkred]
          > >>Obviously the solution is the third example, and I've yet mailed it to
          > >>the webmaster, who is using the second format, 01-01-2000;[/color]
          > > This format cannot be used, since it is regional settings dependent:
          > >
          > > 01-07-2004 means 1st of juli in some pc's but 7th of january in others.
          > >
          > > Do not trust all Italian pc's to have been set "correctly" .
          > >
          > > Do not test on 01-01-2000, since that date will be "correct" in both
          > > circumstances.[/color]
          >
          >This is right; but I was wondering also why both IE and Mozilla accept
          >AAAA/MM/DD and don't accept AAAA-MM-DD format, that is an iso standard;
          >maybe Date.parse is intended to handle only long formats like '15 Jun
          >2004 07:59:19 GMT', and supports other formats only for proprietary
          >extension ?[/color]

          Only an optimist expects software written in the USA to understand
          International Standards. They do not even understand the word
          "International" ; they generally use it as near-synonymous with
          "Foreign". In truth, "Internatio nal" means "the same world-wide
          (including, if possible, the USA)" and "Multi-National" is better for
          "customised , correctly or otherwise, for assumed location".

          [color=blue]
          >Anyway, mine is just an academic discussion about which date standard
          >formats the browsers should and shouldn't accept for Date.parse;
          >certainly the 01-01-2000 format is an ambiguous format and must not be
          >used to calculate dates.[/color]

          It should indeed never be used, except for the Nth day of the Nth month.
          I believe that the true ISO format, YYYY-MM-DD, should be used wherever
          practical; but in javascript using - as a separator is at best unsafe.
          I've "always" recommended and used YYYY/MM/DD in javascript, and have
          never been told of any problem with it.

          Actually, I think I never tried xx-xx-xxxx (knowing xxxx-xx-xx to fail);
          my MSIE4 takes it as MM-DD-YYYY, which is unacceptable.

          A parser should be liberal, but not to the point of accepting ambiguity.
          Ideally, it would accept only "YYYYxMMxDDyHHz MMzSS Zone" in which x y &
          z are any plausible separators; and there would be another form, or an
          optional extra parameter, for specifying the date field order.

          Your "9466812000 00" is of course when Year 2000 started in Italy.
          Canadians get a larger number.

          --
          © 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...