D
Dr J R Stockton
In comp.lang.javascript message <[email protected]
september.org>, Fri, 16 Oct 2009 22:32:44, Garrett Smith
As an answer to the Subject question : ridiculous.
Keep at least that range. If anyone wants a reduced range, the required
change is obvious. This is a FAQ for reading intelligently, not a
library for executing.
"isInRange" is unnecessarily long; "ok" suffices. Better :
if (year<0 || year>9999) { throw ...
It's not nice to have 0/0000 and 9999 coded twice, once as Number and
once in String.
"Throw" should not be used, since how to use it is not obvious and there
is no example in the FAQ.
This is slightly shorter and is faster in Firefox :
mm = (101 + dateInRange.getMonth() + "").slice(-2);
Pedagogically, it would be better to do the lengthening with a function
(or a Method of Number). Such a function would be useful in presenting
times and could be used for currency. This is a general 2-digit Leading
Zero function, and can be simplified (untested) if it is certain that
its argument will not be null or negative :
function LZ(n) { return (n!=null&&n<10&&n>=0?"0":"") + n }
function LZ(n) { return (n<10?"0":"") + n }
Really, the routine should accept all dates within Date Object range.
It is much easier to simplify a general routine than to generalise a
simple routine.
Reverting to the first article of the thread : the comment says what
happens if the string dies not match the format YYYY-MM-DD, but it does
not actually say what happens if the digits supplied are incompatible
with the Gregorian Calendar. At least, change the line to
if (month != date.getMonth() + 1) { // validate numerical values
Goodman, Flanagan, ISO/IEC 16262, ECMA 262 FD5, Asen Bozhilov, the
recently-disparaged old Netscape MAN-type page (generally), even
<http://www.nasa.gov/js/195153main_countdownclock.js> - they all put a
space between "if" & "("; the FAQ should do so likewise. It improves
legibility.
september.org>, Fri, 16 Oct 2009 22:32:44, Garrett Smith
How does this look?
As an answer to the Subject question : ridiculous.
function formatDate(dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999,
yyyy, mm, dd;
if(!isInRange) {
throw RangeError("formatDate: year must be 0000-9999");
}
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return yyyy + "-" + mm + "-" + dd;
}
Keep at least that range. If anyone wants a reduced range, the required
change is obvious. This is a FAQ for reading intelligently, not a
library for executing.
"isInRange" is unnecessarily long; "ok" suffices. Better :
if (year<0 || year>9999) { throw ...
It's not nice to have 0/0000 and 9999 coded twice, once as Number and
once in String.
"Throw" should not be used, since how to use it is not obvious and there
is no example in the FAQ.
This is slightly shorter and is faster in Firefox :
mm = (101 + dateInRange.getMonth() + "").slice(-2);
Pedagogically, it would be better to do the lengthening with a function
(or a Method of Number). Such a function would be useful in presenting
times and could be used for currency. This is a general 2-digit Leading
Zero function, and can be simplified (untested) if it is certain that
its argument will not be null or negative :
function LZ(n) { return (n!=null&&n<10&&n>=0?"0":"") + n }
function LZ(n) { return (n<10?"0":"") + n }
Really, the routine should accept all dates within Date Object range.
It is much easier to simplify a general routine than to generalise a
simple routine.
Reverting to the first article of the thread : the comment says what
happens if the string dies not match the format YYYY-MM-DD, but it does
not actually say what happens if the digits supplied are incompatible
with the Gregorian Calendar. At least, change the line to
if (month != date.getMonth() + 1) { // validate numerical values
Goodman, Flanagan, ISO/IEC 16262, ECMA 262 FD5, Asen Bozhilov, the
recently-disparaged old Netscape MAN-type page (generally), even
<http://www.nasa.gov/js/195153main_countdownclock.js> - they all put a
space between "if" & "("; the FAQ should do so likewise. It improves
legibility.