In comp.lang.javas cript message <es*********@ne ws1.newsguy.com >, Tue, 27
Feb 2007 18:58:46, Matt Kruse <ne********@mat tkruse.composte d:
>In validating, there's no need to check for Leapness until it has been
established that it is a Feb 29 date. But for shorter code one can
compare the date with 28+Leap(Y) .
Code was written years ago and never optimized. Although premature
optimization is unnecessary, the code could certainly stand to be less
verbose in places. A full re-write is on my to do list, and has been for
almost 2 years.
That means that it will not be done.
Here's a start :
if (!Date.prototyp e.getFullYear) {
Date.prototype. getFullYear = function() { var yy=this.getYear ();
return (yy<1900?yy+190 0:yy); }
I've heard of a browser, I think, for which getYear() would currently
return 7. My site has a function getFY which is not much longer and
uses in effect only getYear()%100.
function getFY(D) { var YE // needs full test in all browsers
YE = 1970 + Math.round(D.ge tTime() / 31556952000) // s per Greg yr
return YE + (D.getYear()-YE)%100 }
Note : it is not sensitive to errors on the values of 1970 and
31556952000, since YE only determines the century.
this.isInteger = function(val) {
for (var i=0; i < val.length; i++) {
if ("1234567890".i ndexOf(val.char At(i))==-1) {
return false;
}
}
return true;
};
can probably be
this.isInteger = function(val) ( return ! /\D/.test(val) }
if (interval=='y') { // year
this.setFullYea r(this.getFullY ear()+number);
}
else if (interval=='M') { // Month
this.setMonth(t his.getMonth()+ number);
}
Needs consideration of values that would land on a day that the
destination month does not have. Generally, use the last day of the
month; sometimes, the first of the next month. Wikipedia refers to a
case where a date/time in Feb 29 rounds down before noon and up from
noon.
Stepping weekdays can be optimised for speed by extracting multiples of
5 days and adding 7 days for each. If that is done with an appropriate
version of mod & div, the fractional-week count need only be forwards,
which may be simpler.
>In <http://www.javascriptt oolbox.com/lib/date/examples.php>, the
"Adding Time" demo must have a problem. If I add 7500000 seconds and
0..7 years to 2000-01-01 01:00:00, I should not always get the same
time of day.
Interesting. Have you looked at the code and found where the bug might be? I
have not yet verified myself that there is a bug, but if you have a patch it
would be great.
Apart from your determination of the current year, probably everything
else can be done in Greenwich Time. It is especially obvious, as 3/11
approaches, what problem that annihilates. The UTC methods should be
several times faster; and in at least one case (same date) are not
needed at least for post-1969. (check that)
Math.floor(D1/864e5) == Math.floor(D2/864e5)
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6.
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.