Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9328

Re: setDate()

From Dr J R Stockton <reply1150@merlyn.demon.co.uk>
Newsgroups comp.lang.javascript
Subject Re: setDate()
Date 2011-12-16 22:17 +0000
Organization Home
Message-ID <J8ltmzKEO86OFwRT@invalid.uk.co.demon.merlyn.invalid> (permalink)
References <etdie7d0tt50b7kbo00l0ojfgfggqhj9ui@4ax.com>

Show all headers | View raw


In comp.lang.javascript message <etdie7d0tt50b7kbo00l0ojfgfggqhj9ui@4ax.
com>, Wed, 14 Dec 2011 16:00:12, Gene Wirchenko <genew@ocis.net> posted:

>     I have just about finished my date class.  One of the last
>consequential functions is to add/subtract days from a date.
>
>     In IE9, with Date, one can do
>          SomeDate.setDate(SomeDate.getDate()+n);
>and if there is overflow in the days of the month, the date will be
>normalised.  e.g. 2011-12-31 + 32 = 2012-02-01.
>
>     Is this behaviour that I can count on?  It seems to fail at 366
>days.  I have seen nothing documenting the normalising, but the
>standard says very little about Date's setDate().

Section 15.9.5.37 of the current ECMA standard tells you exactly what
should happen.  Just work through it, step by step.

I have a vague recollection of a system using 16-bit arithmetic for such
things, limiting n to -32768 .. +32767.  It should be extinct.

Read <http://www.merlyn.demon.co.uk/js-datex.htm> &
 <http://www.merlyn.demon.co.uk/js-flaws.htm>.  They prove that very
little can be trusted, in browsers as a whole.  One cannot even rely on
            new Date(Y, 0, 1) == new Date(Y + "/01/01")
for more than the next 23 years.

But the only date bug I know of in IE9 (which I do not have) is in
VBScript.

But that's a bad statement - there is no JavaScript or VBscript in IE9
(if it resembles earlier IE).  Instead, they use script engines shared
by the rest of MS Windows - and those may be updated at any time.  So
one needs to re-test after any script engine update,

If you need to add/subtract days, and to use Y M D, the safest way of
all is to store dates as integers (signed 16-bit will serve for a
working lifetime, except for the occasional Senator) and to have
reliable routines for Y, M, D to/from integer and for local date string
(maybe BC uses FFF dates) to/from integer and for company date string
to/from integer.

In your commercial application, the range of dates you care about is not
great; there are only 146097 days in 400 years Gregorian.  You can test
all possible steps for dates in your career in under a US billion goes,
which will not take too long to try.

-- 
 (c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Back to comp.lang.javascript | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

setDate() Gene Wirchenko <genew@ocis.net> - 2011-12-14 16:00 -0800
  Re: setDate() Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-15 01:09 +0100
    Re: setDate() Gene Wirchenko <genew@ocis.net> - 2011-12-14 18:37 -0800
      Re: setDate() "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-15 12:23 +0000
        Re: setDate() Gene Wirchenko <genew@ocis.net> - 2011-12-15 12:11 -0800
          Re: setDate() "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-15 21:27 +0000
            Re: setDate() Gene Wirchenko <genew@ocis.net> - 2011-12-15 15:26 -0800
              Re: setDate() Swifty <steve.j.swift@gmail.com> - 2011-12-16 06:29 +0000
              Re: setDate() "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-16 08:41 +0000
  Re: setDate() Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-15 12:28 +0100
    Re: setDate() Gene Wirchenko <genew@ocis.net> - 2011-12-15 12:12 -0800
      Re: setDate() "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-15 21:39 +0000
        Re: setDate() Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-15 23:25 +0100
          Re: setDate() Jake Jarvis <pig_in_shoes@yahoo.com> - 2011-12-15 23:28 +0100
          Re: setDate() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-17 00:04 +0100
            Re: setDate() Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-17 00:06 +0100
        Re: setDate() Swifty <steve.j.swift@gmail.com> - 2011-12-16 06:35 +0000
          Re: setDate() "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-16 08:29 +0000
  Re: setDate() Dr J R Stockton <reply1150@merlyn.demon.co.uk> - 2011-12-16 22:17 +0000

csiph-web