Re: FAQ Topic - How do I format a date with javascript? (2009-06-01)



In comp.lang.javascript message <h0uruq$lrq$1@xxxxxxxxxxxxx
september.org>, Fri, 12 Jun 2009 17:30:12, Garrett Smith
<dhtmlkitchen@xxxxxxxxx> posted:
Dr J R Stockton wrote:
In comp.lang.javascript message <h0t2oi$je2$1@xxxxxxxxxxxxx
september.org>, Fri, 12 Jun 2009 01:14:07, Garrett Smith
<dhtmlkitchen@xxxxxxxxx> posted:


The entry:

"11.1 How do I get my browser to report javascript errors?"

covers the subject of browsers reporting errors.
But it says nothing about the effect of calling throw.

Technically, |throw| is a Statement, and is not called.

Exception handling could find its place in the FAQ.


It would also be noticed to return "". Not an ISO-8601 format, but
noticeable.

But sometimes easy to miss. Consider a Table in which there is a column
of dates some of which are, for other reasons, not present. An empty
string does not look like a string, when written.



The ISO 8601 Extended format can be understood internationally, without
xxx ^An (there are more than one) (don't use Any[0])
world-wide ---> xxxxxxxxxxxxxx
ISO 8601 Extended formats can be...
No; that definitely implies that all of them can be.

or

[Most|Common|Many] 8601 Extended formats can be...

or even

Many common ISO 8601 Extended formats can be..
Obvious minor variations such as year outside 0000-9999 and the
presence
of time fields apart, there are not enough extended formats to justify
"Many".


So going with "Common ISO 8601 Extended formats..."

To be convincing, you will need to explain here which common ones fit
the rest of what you say. In the FAQ context, one needs something
stronger than "in each country, there will be some who understand".
Take today's date in the various ISO extended formats to different young
shop assistants, asking what's that? Probably, "YYYY-MM-DD" will be
recognised as a funny way of writing the date; the others may well be
considered as some strange sort of part number.





No. A suffix is below, an index or superfix above - as in HTML
<sub>

that's subscript.
<sup>.

superscript.

Yes. I wrote what I meant to write; there's a read error. There's no
semantic difference in this context in English between "fix" and
"script".


A prefix is before, a postfix is after, both on-the-line.
Suffix in this context will be understood, but postfix is correct.



I wrote, legibly enough, "at the very minimum", in order to show
only
what was needed for validation by checking getMonth to work. But why
not use \D, which correctly accepts ALL whitespace varieties and permits
finding dates in strings containing other non-digits? The purpose is to
validate the date indicated, not the ISO compliance.


It expands the functionality of what the method does to something it is
not designed or tested for. I don't want the method to guaranteed that.
Allowing leading/trailing space only keeps it simpler.

You need to learn to write English so that it can be unambiguously
parsed by others. That can be read as either of
Allowing (leading/trailing space only) keeps it simpler.
Allowing leading/trailing space (only keeps it simpler).


Including "\D" would match: YYYY-MM-DDT and the function would not do
anything with anything following that "T".

That's right. But, at the ends of YYYY-MM-DD, it should be \D* . One
wants to reject a digit in that end position; there is no need to reject
anything else.

The "inRange" was to indicate that there is a range. That range is not
indicated, but it is 0000-9999.

Do you have in mind a better name for the function that would indicate
that?

Str. It is expected to be a string; it is only hoped that it will
represent a good date.

formatDateToExtendedYYYYMMDD
- hard to read with all the long run of upper case letters. Even with
camelCase, it is an eyesore: formatDateToExtendedYyyyMmDd.

Underscore could possibly work:
formatDateToYYYY_MM_DD

- hard to read and does not indicate range.

Call it YYYY_MM_DD.

var date = new Date(NaN),
isoExp, parts;
isoExp = /^\s*([\d]{4})-(\d\d)-(\d\d)\s*$/;
No general need to check field lengths, except for not exceeding DD.

The pattern is YYYY-MM-DD, and that is what the regexp checks (and
allows whitespace on the ends).

One needs to consider whether the code should check that the string is a
correct production of the ISO rules or should check that a valid date is
represented more-or-less in the ISO manner. For typed input, there's no
need to enforce rigour in the separators; "YYYY-MM-DD" is ISO but it's
easier to type "YYYY MM DD".

In fact the RegExp should be considered as an argument to the process of
typing the function.


function parseISO8601( dateStringInRange ) {
var date = new Date(NaN),
isoExp, parts;
isoExp = /^\s*([\d]{4})-(\d\d)-(\d\d)\s*$/;
parts = isoExp.exec(dateStringInRange);

if(parts) {
^
That looks like a call of the function 'if'. Goodman uses a space.
Flanagan uses a space.

date.setFullYear(parts[1], parts[2] - 1, parts[3]);
if( parts[1] != date.getMonth() + 1 ) {
date.setTime(NaN);
} else {
date.setHours(0, 0, 0, 0);
}
}
return date;
}

There's little point in reviewing untested code, as that obviously is.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL: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)
.



Relevant Pages