Numeric Date Validation

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

    #16
    Re: Numeric Date Validation

    Hold on a sec. First of all, your expression (Method 1)
    [color=blue]
    > ( !( ayear % 4 ) && ( ayear % 100 )) || !( ayear % 400 )[/color]

    is saying a year is a leap year iff
    ((it's divisible by 4) AND (it's not divisible by 100)) OR
    (it's divisible by 400).

    But that's the same as (Method 2):
    a year is a leap year iff
    (it's divisible by 4) AND ((it's not divisible by 100) OR
    (it's divisible by 400))

    In other words, from a logical point of view, you don't
    NEED the grouping parentheses. But they DO make
    a difference (theoretically, anyway. YOU can do the testing).
    Let's just check out how many times each condition gets
    executed.

    Method 1:
    Year not divisible by 4: first and last expressions
    Year divisible by 4/ not by 100: first two
    Year divisible by 400: all three

    Method 2:
    Year not divisible by 4: first one only
    Year divisible by 4/ not by 100: first two
    Year divisible by 400: all three

    So in 75% of all cases, Method 2 will have only one test
    to two for Method 1.

    But wait, there's more. Since you've come to the right
    newsgroup, tonight only, we'll throw in a special simplification,
    to get rid of not one, but two of those pesky modulo operators.
    Who needs extra divisions, after all? So here you have it -
    Method 2, simplified:

    isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))


    From Bit Tiddle City,
    Csaba Gabor

    PS. If you leave out the != 0 part in the second condition, your result
    in the case of leap years not divisible by 400 will be numeric (nonzero -
    the result of ayear % 25) instead of boolean true, which would be OK
    for most of my purposes.


    Comment

    • Michael Winter

      #17
      Re: Numeric Date Validation

      On Tue, 20 Apr 2004 03:53:04 +0200, Csaba Gabor <news@CsabaGabo r.com>
      wrote:
      [color=blue]
      > Hold on a sec. First of all, your expression (Method 1)[/color]

      Actually, my expression was method 2. The first two expressions I stated
      were representations of Dennis' test once operator precedence was applied.
      My proposed expression was the last in my first post.
      [color=blue][color=green]
      >> ( !( ayear % 4 ) && ( ayear % 100 )) || !( ayear % 400 )[/color]
      >
      > is saying a year is a leap year iff
      > ((it's divisible by 4) AND (it's not divisible by 100)) OR
      > (it's divisible by 400).
      >
      > But that's the same as (Method 2):
      > a year is a leap year iff
      > (it's divisible by 4) AND ((it's not divisible by 100) OR
      > (it's divisible by 400))
      >
      > In other words, from a logical point of view, you don't
      > NEED the grouping parentheses. But they DO make
      > a difference (theoretically, anyway. YOU can do the testing).
      > Let's just check out how many times each condition gets
      > executed.
      >
      > Method 1:
      > Year not divisible by 4: first and last expressions
      > Year divisible by 4/ not by 100: first two
      > Year divisible by 400: all three
      >
      > Method 2:
      > Year not divisible by 4: first one only
      > Year divisible by 4/ not by 100: first two
      > Year divisible by 400: all three
      >
      > So in 75% of all cases, Method 2 will have only one test
      > to two for Method 1.[/color]

      I hadn't thought about it before (I was just rattling off code without
      much thought, as I said earlier), but during testing I noticed that not
      all results were boolean. When I examined the expression, I came to your
      same conclusion.
      [color=blue]
      > But wait, there's more. Since you've come to the right
      > newsgroup, tonight only, we'll throw in a special simplification,
      > to get rid of not one, but two of those pesky modulo operators.
      > Who needs extra divisions, after all? So here you have it -
      > Method 2, simplified:
      >
      > isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))[/color]

      I realised that Dr Stockton was indicating more than what I responded to,
      but I was too tired to consider what. My (still) tired brain thanks you. :)

      The first bitwise operation was obvious in its effect (a common enough
      optimisation), but I couldn't think why the third was using 15. I then
      noticed you changed the second to 25. A prime example of why I normally
      avoid thinking too hard in the morning.

      Mike


      [1] I didn't expect && to operate the same way as || for non-boolean
      types. Not having used it in that manner before, whereas I often use ||
      for setting default values, I don't suppose there was much reason to.

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      • Dr John Stockton

        #18
        Re: Numeric Date Validation

        JRS: In article <Xns94D17776409 2eejj99@194.109 .133.29>, seen in
        news:comp.lang. javascript, Evertjan. <exjxw.hannivoo rt@interxnl.net >
        posted at Mon, 19 Apr 2004 22:43:50 :
        [color=blue]
        >Why confine onself merely to history, what about prehistoric dates ?[/color]

        Javascript will handle back past BC 270000, using proleptic Gregorian;
        my site has the essentials for Julian. "Actual" calendars other than
        those are either too troublesome or not interesting enough, though I
        have some Hebrew calendar stuff in Pascal.

        [color=blue]
        >These sub 100 year inputs can be expanded
        >to the "nearest" 4 digit year numbers
        >
        >[again intentionally educationally non optimized]:
        >
        >if (Y>=0 && Y<35) Y += 2000
        >if (Y>=35 && Y<100) Y += 1900[/color]


        But some people will actually want to refer to years before AD 100.

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

        • Dr John Stockton

          #19
          Re: Numeric Date Validation

          JRS: In article <opr6p74tap5vkl cq@news-text.blueyonder .co.uk>, seen in
          news:comp.lang. javascript, Michael Winter <M.Winter@bluey onder.co.invali
          d> posted at Mon, 19 Apr 2004 23:12:30 :
          [color=blue]
          >A simple test, taking years from 0 to 10000[1], shows no difference in
          >result. However, there is a *very* slight difference in execution time,
          >which makes mine faster (even when forcing the result to boolean using
          >!!(expr)).[/color]
          [color=blue]
          >[1] I realise that a reasoned choice would be more desirable, but it
          >should suffice.[/color]

          Since the Calendar repeats every 400 years, 1 to 10000 would be better,
          but only in principle.

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

          • Dr John Stockton

            #20
            Re: Numeric Date Validation

            JRS: In article <40848486@andro meda.datanet.hu >, seen in
            news:comp.lang. javascript, Csaba Gabor <news@CsabaGabo r.com> posted at
            Tue, 20 Apr 2004 03:53:04 :
            [color=blue]
            >isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))[/color]

            !( ayear & 3 || !( ayear & 15 ) && ayear % 25 )

            omits the != 0 but still gives a Boolean result in every case; it has
            the same number of other operations.
            Now tested in <URL:http://www.merlyn.demo n.co.uk/js-date4.htm#MaYL> , but
            not timed.
            [color=blue]
            >From Bit Tiddle City,[/color]

            Tiddle != Twiddle

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's 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

            • Mick White

              #21
              Re: Numeric Date Validation

              Evertjan. wrote:
              [color=blue]
              >
              > function dateOK(Y,M,D){
              > var da = new Date(Y,M-1,D)
              > return Y == da.getFullYear( ) &&
              > M-1 == da.getMonth() &&
              > D == da.getDate()
              > }[/color]

              It would validate "dateOK(2000,20 00,2000)", would it not?
              Mick
              [color=blue]
              >
              > This also checkes for decimals, strings, negatives, etc.
              > correctly validating strings are allowed, like year '02004',month '3.00'
              > returns false if year<100
              >[/color]

              Comment

              • Lasse Reichstein Nielsen

                #22
                Re: Numeric Date Validation

                Mick White <mwhite13@roche ster.rr.com> writes:
                [color=blue]
                > Evertjan. wrote:
                >[color=green]
                >> function dateOK(Y,M,D){
                >> var da = new Date(Y,M-1,D)
                >> return Y == da.getFullYear( ) &&
                >> M-1 == da.getMonth() &&
                >> D == da.getDate()
                >> }[/color]
                >
                > It would validate "dateOK(2000,20 00,2000)", would it not?[/color]

                Try (and see that it doesn't).

                /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

                • Richard Cornford

                  #23
                  Re: Numeric Date Validation

                  Lasse Reichstein Nielsen wrote:
                  <snip>> var __months = [,31,28,31,30,31 ,30,31,31,30,31 ,30,31][color=blue]
                  > function DateOK2(Y, M, D) {
                  > var ML = __months[M];
                  > return D>0 && ML && (D<=ML ||
                  > D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) }[/color]
                  <snip>

                  The comparison operation on - ML - reminded me that a - <= - comparison
                  of an object with a number will call the object's - valueOf - method,
                  and so the possibility of putting an object with an overridden -
                  valueOf - method into index 2 of the months length array and have that
                  method return 28 or 29 based on the year:-

                  var DateOK3 = (function(){
                  var _y;
                  var __months = [
                  NaN,31,
                  {valueOf:functi on(){
                  return (((_y&3)||((_y& 15)&&(!(_y%25)) ))?28:29);
                  }},
                  31,30,31,30,31, 31,30,31,30,31
                  ];
                  return (function(Y, M, D){
                  _y = Y;
                  return (D > 0) && (D <= __months[M]);
                  });
                  })();

                  Which takes advantage of undefined values from - __months[M] - type
                  converting to NaN for the comparison and so returns false from - (D <=
                  __months[M]) - when - M - is out of the appropriate range and has the
                  merit of always returning a boolean value.

                  Richard.


                  Comment

                  • Csaba Gabor

                    #24
                    Re: Numeric Date Validation

                    > >isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))[color=blue]
                    >
                    > !( ayear & 3 || !( ayear & 15 ) && ayear % 25 )[/color]

                    Hmmm, that's very interesting. My first inclination was to say that
                    swapping those last two arguments was going to lead the last one being
                    evaluated (almost) all the time (since it's checking for divisibility by
                    400,
                    only true 1/400 of the time) but that's not correct since the 25 is doing
                    much of the work. That being the case, the situation becomes murkier
                    and depends much more on the efficiency of the various operations.
                    In particular, the last operation (the presumed expensive Mod 25)
                    is only going to be done 1/16th of the time as opposed to the 1/4 on
                    the top line. Now I'm real curious as to which wins out on time, if either.

                    Thanks for that post,

                    Csaba,
                    a bit toddler


                    Comment

                    • Dr John Stockton

                      #25
                      Re: Numeric Date Validation

                      JRS: In article <4085d7ca@andro meda.datanet.hu >, seen in
                      news:comp.lang. javascript, Csaba Gabor <news@CsabaGabo r.com>
                      posted at Wed, 21 Apr 2004 04:00:36 :
                      [color=blue][color=green][color=darkred]
                      >> >isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))[/color]
                      >>
                      >> !( ayear & 3 || !( ayear & 15 ) && ayear % 25 )[/color]
                      >
                      >Hmmm, that's very interesting. My first inclination was to say that
                      >swapping those last two arguments was going to lead the last one being
                      >evaluated (almost) all the time (since it's checking for divisibility by
                      >400,
                      >only true 1/400 of the time) but that's not correct since the 25 is doing
                      >much of the work. That being the case, the situation becomes murkier
                      >and depends much more on the efficiency of the various operations.
                      >In particular, the last operation (the presumed expensive Mod 25)
                      >is only going to be done 1/16th of the time as opposed to the 1/4 on
                      >the top line. Now I'm real curious as to which wins out on time, if either.[/color]


                      The tests will be performed on order (assumption). The most effective
                      tests are those where the two possible results are most nearly equally
                      probable (general rule). 4 < 16 < 25.


                      PII/300, MSIE4 :-


                      1000 // ; var D0, D1, D2, J, isLeapYear, ayear

                      D0 = new Date()
                      for (J=0 ; J<K ; J++) for (ayear=2000 ; ayear<2400 ; ayear++)
                      isLeapYear = !( ayear & 3 ) && (( ayear % 25 != 0 ) || !( ayear & 15 ))
                      D1 = new Date()
                      for (J=0 ; J<K ; J++) for (ayear=2000 ; ayear<2400 ; ayear++)
                      isLeapYear = !( ayear & 3 || !( ayear & 15 ) && ayear % 25 )
                      D2 = new Date()
                      for (J=0 ; J<K ; J++) for (ayear=2000 ; ayear<2400 ; ayear++)
                      isLeapYear = !( ayear & 3 )
                      D3 = new Date()

                      X = [D1-D0, D2-D1, D3-D2]


                      Executing in <URL:http://www.merlyn.demo n.co.uk/js-quick.htm>, typical
                      results are 3680,3620,3240 3740,3620,3190. About half of the time is
                      taken by loop overhead; the difference is a few percent, with mine being
                      faster.

                      Unveiling the var makes things faster.

                      The RmEv button, used above, gives much shorter times that the Eval
                      button - so thanks to Richard, who can possibly explain why!

                      ....

                      <URL:http://www.merlyn.demo n.co.uk/js-date4.htm#MaYL> now includes speed
                      testing (LeapSloth). It was designed on a system with time resolution
                      about 55 ms x 300 MHz CPU cycles; those with relatively faster CPUs may
                      need, and should be willing, to increase the year range tested by a
                      multiple of 400. Routines not using Objects are *much* quicker.

                      --
                      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                      <URL:http://jibbering.com/faq/> Jim Ley's 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

                      • Richard Cornford

                        #26
                        Re: Numeric Date Validation

                        Dr John Stockton wrote:
                        <snip>[color=blue]
                        > The RmEv button, used above, gives much shorter times that the Eval
                        > button - so thanks to Richard, who can possibly explain why![/color]
                        <snip>

                        As I recall the point of the - remoteEval - was to avoid evaling the
                        expression in a scope derived from the event handling function's
                        execution context because the event handling function is provided with a
                        custom scope chain that may modify the interpretation of identifiers
                        used in the code that is evaled. It is probably this custom scope chain
                        that is influencing the execution speed of the code in the two contexts.

                        The execution context of the - remoteEval - function will only include
                        its Activation/Variable object and the global object, so any identifier
                        resolution will only involve the examination of these two objects.

                        IE adds the - document -, the form element and the - this - object to
                        the scope chain of its event handlers (Mozilla adds more objects), so
                        the resolution of an identifier that refers to a global variable will
                        necessitate a search of all of these objects (in addition to the
                        Activation/Variable object from the event handler's execution context
                        and the global object). The speed of resolution of local variables will
                        depend on whether the added objects are above the event handler's
                        Activation/Variable object in the scope chain (as would be the case if
                        they had been added using the - with - statement) or whether they are
                        below it.

                        On the other hand each test seems to be doing about as much identifier
                        resolution (except the last) so the relative speed results are still
                        meaningful in either test.

                        Richard.


                        Comment

                        • Dr John Stockton

                          #27
                          Re: Numeric Date Validation

                          JRS: In article <c647h2$hea$1$8 302bc10@news.de mon.co.uk>, seen in
                          news:comp.lang. javascript, Richard Cornford
                          <Richard@litote s.demon.co.uk> posted at Tue, 20 Apr 2004 23:17:04 :[color=blue]
                          >Lasse Reichstein Nielsen wrote:
                          ><snip>> var __months = [,31,28,31,30,31 ,30,31,31,30,31 ,30,31][color=green]
                          >> function DateOK2(Y, M, D) {
                          >> var ML = __months[M];
                          >> return D>0 && ML && (D<=ML ||
                          >> D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) }[/color][/color]

                          To avoid getting a result which is neither true nor false (but is
                          equivalent to false), I put !! before ML.
                          [color=blue]
                          ><snip>
                          >
                          >The comparison operation on - ML - reminded me that a - <= - comparison
                          >of an object with a number will call the object's - valueOf - method,
                          >and so the possibility of putting an object with an overridden -
                          >valueOf - method into index 2 of the months length array and have that
                          >method return 28 or 29 based on the year:-
                          >
                          >var DateOK3 = (function(){
                          > var _y;
                          > var __months = [
                          > NaN,31,
                          > {valueOf:functi on(){
                          > return (((_y&3)||((_y& 15)&&(!(_y%25)) ))?28:29);
                          > }},
                          > 31,30,31,30,31, 31,30,31,30,31
                          > ];
                          > return (function(Y, M, D){
                          > _y = Y;
                          > return (D > 0) && (D <= __months[M]);
                          > });
                          >})();
                          >
                          >Which takes advantage of undefined values from - __months[M] - type
                          >converting to NaN for the comparison and so returns false from - (D <=
                          >__months[M]) - when - M - is out of the appropriate range and has the
                          >merit of always returning a boolean value.[/color]

                          Remarkable code. But it appears to work (though seems, tested with
                          2002-02-29, *marginally* slower than the best of the others). It's
                          testable in <URL:http://www.merlyn.demo n.co.uk/js-dates4.htm#DVal >.

                          Alas, unless I wrap the above in a "normal" function, it's not amenable
                          to my usual code display technique.


                          Thanks for your RmEv explanation.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                          <URL:http://jibbering.com/faq/> Jim Ley's 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

                          • Richard Cornford

                            #28
                            Re: Numeric Date Validation

                            "Dr John Stockton" wrote:[color=blue]
                            > Richard Cornford posted:[color=green]
                            >>Lasse Reichstein Nielsen wrote:
                            >><snip>> var __months = [,31,28,31,30,31 ,30,31,31,30,31 ,30,31][color=darkred]
                            >>> function DateOK2(Y, M, D) {
                            >>> var ML = __months[M];
                            >>> return D>0 && ML && (D<=ML ||
                            >>> D==29 && Y%4==0 && (Y%100!=0 || Y%400==0) ) }[/color][/color]
                            >
                            > To avoid getting a result which is neither true nor false (but is
                            > equivalent to false), I put !! before ML.
                            >[color=green]
                            >>The comparison operation on - ML - reminded me that a - <= -
                            >>comparison of an object with a number will call the object's
                            >>- valueOf - method, and so the possibility of putting an object
                            >>with an overridden - valueOf - method into index 2 of the months
                            >>length array and have that method return 28 or 29 based on the
                            >>year:-
                            >>var DateOK3 = (function(){
                            >> var _y;
                            >> var __months = [
                            >> NaN,31,
                            >> {valueOf:functi on(){
                            >> return (((_y&3)||((_y& 15)&&(!(_y%25)) ))?28:29);
                            >> }},
                            >> 31,30,31,30,31, 31,30,31,30,31
                            >> ];
                            >> return (function(Y, M, D){
                            >> _y = Y;
                            >> return (D > 0) && (D <= __months[M]);
                            >> });
                            >>})();
                            >>
                            >>Which takes advantage of undefined values from - __months[M] - type
                            >>converting to NaN for the comparison and so returns false from -
                            >>(D <= __months[M]) - when - M - is out of the appropriate range
                            >>and has the merit of always returning a boolean value.[/color]
                            >
                            > Remarkable code. But it appears to work (though seems, tested with
                            > 2002-02-29, *marginally* slower than the best of the others). It's
                            > testable in <URL:http://www.merlyn.demo n.co.uk/js-dates4.htm#DVal >.[/color]

                            Yes, my speed tests give Lasse's the edge (4% or so, with a range of
                            dates some of which are invalid).

                            On the other hand, I was looking a Lasse's logic and I think it can be
                            faster still:-

                            return (D <= __months[M])? (D > 0) :
                            (D==29) && (M==2) && !( ((Y&3)||((Y&15) &&(!(Y%25))) ) );

                            As out of range - M - values return undefined from - __months[M] -,
                            which type-convert to NaN for the comparison, only in range - M - values
                            can produce true results from - (D <= __months[M]) - so the - (D > 0) -
                            test can be moved so that it only applies when the comparison returns
                            true.

                            The only condition where a false value for - (D <= __months[M]) - will
                            eventually leave the function returning true is when D == 29, M ==2 and
                            the year makes February 29 days long. If - D - was less than zero is
                            would not equal 29. There is also no longer any need to test the value
                            returned from - __months[M] - separately by type converting it as it
                            would only type-convert to true if - M == 2 -, removing the need for the
                            ML local variable and the assignment operation..

                            Finally, using the expression that I used to positively select day
                            lengths of 28 for February, and applying the NOT operator allows that
                            final expression to return a boolean value even though two of its
                            intermediate values are numeric.

                            I make it 6-10% faster than Lasse's original (with !!ML). The version I
                            was using for testing was:-

                            var DateOK2b = (function(){
                            var __months = [NaN,31,28,31,30 ,31,30,31,31,30 ,31,30,31]
                            return (function(Y, M, D){
                            return (D <= __months[M])? (D > 0) :
                            (D==29) &&
                            (M==2) &&
                            !( ((Y&3)||((Y&15) &&(!(Y%25))) ) );
                            });
                            })();
                            [color=blue]
                            > Alas, unless I wrap the above in a "normal" function, it's not
                            > amenable to my usual code display technique.[/color]
                            <snip>

                            It isn't strictly necessary that the __months array be held in a
                            closure. I like the idea of bundling the two together and keeping
                            __months free from external influences. It might also be slightly faster
                            to resolve the identifier - __months - in the closure as the global
                            object has many more properties to be considered. But that isn't going
                            to help when it comes to displaying the string value of the resulting
                            (inner) function object.

                            Richard.


                            Comment

                            • Dr John Stockton

                              #29
                              Re: Numeric Date Validation

                              JRS: In article <c6dqgs$41v$1@h ercules.btinter net.com>, seen in
                              news:comp.lang. javascript, Richard Cornford <richard@litote s.demon.co.uk>
                              posted at Sat, 24 Apr 2004 13:36:28 :[color=blue]
                              >
                              >I make it 6-10% faster than Lasse's original (with !!ML). The version I
                              >was using for testing was:-
                              >
                              >var DateOK2b = (function(){
                              > var __months = [NaN,31,28,31,30 ,31,30,31,31,30 ,31,30,31]
                              > return (function(Y, M, D){
                              > return (D <= __months[M])? (D > 0) :
                              > (D==29) &&
                              > (M==2) &&
                              > !( ((Y&3)||((Y&15) &&(!(Y%25))) ) );
                              > });
                              >})();[/color]

                              OK. But && M==2 seems superfluous. Should NaN (instead of blank) be
                              faster?

                              It's not obvious how best to speed-test - one should cover all 1461 good
                              dates in 4 years, or 146097 in 400, but how many & which bad ones?

                              I wonder why the using Date Object is so slow; possibly in order to
                              accommodate out-of-range fields.


                              I've been re-looking at Zeller's Concordance (and have been in
                              communication with one of his great-grandsons). In converting Y M D to
                              day-of-week, Zeller separated out the century, which seems simpler for
                              manual arithmetic; but, at least on a computer, ISTM better not to :-
                              <URL:http://www.merlyn.demo n.co.uk/zeller-c.htm#DDCC> boxes 1 & 3
                              Box 3 gives a much faster way of converting Y M D to a day count than
                              using a Date Object (at least, in my system).

                              That function is repeated at
                              <URL:http://www.merlyn.demo n.co.uk/dayscale.htm#CD D> Box 1
                              and Box 2 gives an inverse. ISTM unlikely that one can do much better
                              for the forward function; but the inverse might be improvable ....

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

                              • Dennis Marks

                                #30
                                Re: Numeric Date Validation

                                Your discussion on speed of date validation is very interesting. Maybe
                                you can get my date calculator down to one single line ; -)

                                --
                                Dennis Marks

                                To reply change none to dcsi.


                                -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
                                http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
                                -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

                                Comment

                                Working...