Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith <dhtmlkitchen@xxxxxxxxx>
- Date: Thu, 11 Jun 2009 09:13:27 -0700
Dr J R Stockton wrote:
In comp.lang.javascript message <h0no2u$de9$1@xxxxxxxxxxxxx[...]
september.org>, Wed, 10 Jun 2009 00:41:15, Garrett Smith
<dhtmlkitchen@xxxxxxxxx> posted:
Putting that in means that the effect of throw needs to be described.
"throw" is a common flow-of-control term in programming. It could be
listed in the glossary, if such thing is to be created.
What does 9075 say about the "T" separator for time? The entry mentions
that "T" may be omitted.
ISO 8601 says "T" can be omitted, but is not thought to mean that; "T"
can be replaced by a space.
An Extended ISO 8601 Date format YYYY-MM-DD can be parsed to a local
To me, "parse" implies a reasonable degree of format checking, such as
(at least) using a moderately rigorous RegExp. Here and in the function
name, "read" would be better.
Date with the following:-
function parseISO8601( dateString ) {
var parts = dateString.split(/-/),
// 0-based month for Date.
mm = +parts[1] - 1,
date = new Date;
date.setFullYear(parts[0], mm, parts[2]);
return date;
}
The + is not needed. The code does not handle negative years. The
function name implies that it will parse all ISO 8601 date strings; at
least, YYYY-MM-DD or Y-M-D should be in comment.
Right, conversion to number happens with "-".
"3" - 2; // 1.
Fixed that and added a doc comment.
Method parseISO8601 has a choice for handling a cases with
dateStringInRange.
1) return an invalid date or 2) throw an error.
An invalid date keeps the code a tiny bit smaller. It is like returning
null however, the returned object is a Date, so it does not require a
null check.
OTOH, throwing an error would require the caller to either a) perform
pre-parse test, or b) wrap the call in try-catch. I don't like either of
these.
I think an invalid date is the better solution here. The caller can
always check it with:-
if(isNaN(invalidDate));
If I am going to go with that, I can also use regexp.exec(
dateStringInRange ) to avoid error.
The Date2 object's new Date2(String) also can read week numbering and
ordinal dates.
The code adds to the date supplied the time that it is executed, more or
less.
One should not use new Date[()] unless at least some of the current
date/time is required. Using new Date(0) is considerably faster, and
means that the return value is fully determinate. Here in the UK it
will have a time part of 00:00:00.0 UTC, but setFullYear seems to handle
that as desired even if the input date is Summer. Away from the UK time
zone I think the result will contain a time part equal or opposite to
the Winter Offset.
Use, therefore, setHours(0,0,0,0).
OK.
Test in locations on either side of the UK zone and of the equator (any
sufficiently South Americans here?), and, since new Date(0) can give Dec
31, include testing with a February date. In fact, new Date(864e6)
might be better; well away from month-end. And, in test, try 200*864e5;
that could flush an Australian bug.
I don't quite understand your testing methodology. It sounds like it would require changing the computer's time zone. Did it get that right?
NaN should generate an invalid date in any time zone, so what you wanted to test would not apply here, right?
Substituting to use new Date("YYYY/MM/DD") is much simpler and safer.
Moreover, that can also read as GMT by merely appending " GMT", and can
read with a known Time Offset, if care is taken to substitute only the
separators and not the sign of the offset.
I can't recommend relying on undocumented implementation extensions.
Not here:
http://bclary.com/2004/11/07/#a-15.9.4.1
or here:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/parse
or here:
http://msdn.microsoft.com/en-us/library/cd9w2te4(VS.85).aspx
Taking all of the suggestions, the new draft follows:-
================================================================
4.1 How do I write and read a date with javascript?
The ISO 8601 Extended format can be understood internationally, without ambiguity.
A local Date object where year >= 0 can be formatted to a common ISO 8601 format YYYY-MM-DD with:-
/** Formats a Date to YYYY-MM-DD, compatible with both
* ISO 8601 and ISO/IEC 9075-2:2003 (E) (SQL 'date' type).
* @param {Date} dateInRange year 0000 to 9999.
* @throws {Error} if the year is < 0.
*/
function formatDate(dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = year > 0 && year < 9999,
yyyy, mm, dd;
if(!isInRange ) {
throw Error("year must be 0000-9999");
}
yyyy = padLeft(year, 4, "0");
mm = padLeft(dateInRange.getMonth() + 1, 2, "0");
dd = padLeft(dateInRange.getDate(), 2, "0");
return yyyy + "-" + mm + "-" + dd;
}
/**
* @param {string} input: input value converted to string.
* @param {number} size: desired length of output.
* @param {string} ch: single character to prefix to s.
*/
function padLeft(input, size, ch) {
var s = input + "";
while (s.length < size) {
s = ch + s;
}
return s;
}
Never use a local date/time for a non-local event. Instead, use UTC, as in ISO 8601 YYYY-MM-DDThh:mm:ssZ ( Z is the only letter suffix).
The T may be omitted where doing so would not cause ambiguity. For an SQL date, it must be replaced by a single space.
For a local date/time with time offset, to unambiguously indicate a particular instant, use ISO 8601 format YYYY-MM-DDThh:mm:ss±hh:mm.
An Extended ISO 8601 Date format YYYY-MM-DD can be parsed to a local Date with the following:-
/**Parses string formatted as YYYY-MM-DD to a Date object.
* If the supplied string does not match the format, an
* invalid Date is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Native Date object representing the string.
*/
function parseISO8601( dateStringInRange ) {
var date = new Date(NaN),
isoExp, parts;
isoExp = /^\s*([\d]{4})-(0[1-9]|1[0-2])-([0-2][0-9]|(?:3[0|1]))\s*$/;
parts = isoExp.exec(dateStringInRange);
if(parts) {
date.setFullYear(parts[1], parts[2] - 1, parts[3]);
date.setHours(0,0,0,0);
}
return date;
}
See also:
* ECMA-262 Date.prototype, s. 15.9.4.2
* http://en.wikipedia.org/wiki/ISO_8601
* ISO 8601:2004(E)
* http://www.merlyn.demon.co.uk/js-date9.htm
================================================================
The entry is long.
Garrett
--
The official comp.lang.javascript FAQ:
http://jibbering.com/faq/
.
- Follow-Ups:
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- References:
- FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: FAQ server
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Osmo Saarikumpu
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: John G Harris
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Garrett Smith
- Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- From: Dr J R Stockton
- FAQ Topic - How do I format a date with javascript? (2009-06-01)
- Prev by Date: Re: passing parameters with setTimeout()
- Next by Date: Re: ClientScript working in IE but not Mozilla
- Previous by thread: Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- Next by thread: Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)
- Index(es):
Relevant Pages
|
Loading