From: FAQ server on
-----------------------------------------------------------------------
FAQ Topic - How can I create a Date from a String?
-----------------------------------------------------------------------

An Extended ISO 8601 local Date format ` YYYY-MM-DD ` can be parsed to a
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 (value NaN) is returned.
* @param {string} dateStringInRange format YYYY-MM-DD, with year in
* range of 0000-9999, inclusive.
* @return {Date} Date object representing the string.
*/
function parseISO8601(dateStringInRange) {
var isoExp = /^\s*([\d]{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN), month,
parts = isoExp.exec(dateStringInRange);

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


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

From: Dr J R Stockton on
In comp.lang.javascript message <4ac928fb$0$289$14726298(a)news.sunsite.dk
>, Sun, 4 Oct 2009 23:00:03, FAQ server <javascript(a)dotinternet.be>
posted:

>FAQ Topic - How can I create a Date from a String?

That makes no sense; should be "Date Object". And the string is not
*used* to *make* the Object; the date which the string represents
defines the value of the Object.

FAQ Topic - How can I obtain a Date Object with value set by a String?


>An Extended ISO 8601 local Date format ` YYYY-MM-DD ` can be parsed to a
>Date with the following:-

No; the format YYYY-MM-DD does not represent any date.

A Date Object representing the content of a string in Extended ISO 8601
local Date format ` YYYY-MM-DD ` can be obtained using 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 (value NaN) is returned.
>* @param {string} dateStringInRange format YYYY-MM-DD, with year in
>* range of 0000-9999, inclusive.
>* @return {Date} Date object representing the string.
>*/
>function parseISO8601(dateStringInRange) {
>var isoExp = /^\s*([\d]{4})-(\d\d)-(\d\d)\s*$/,

Do the square brackets serve any useful purpose?

> date = new Date(NaN), month,
> parts = isoExp.exec(dateStringInRange);
>
>if(parts) {
>month = +parts[2];
>date.setFullYear(parts[1], month - 1, parts[3]);
>if(month != date.getMonth() + 1) {
> date.setTime(NaN);
>} else {
> date.setHours(0, 0, 0, 0);

That setHours should not be necessary; and the ECMA standard should make
it so.

I'd slightly prefer, while setHours is present, to use
if ( == ) rather than if ( != ) .


date.setFullYear(parts[1], --month, parts[3]);
if ( month != date.getMonth() ) { // seems clearer


--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
From: John G Harris on
On Mon, 5 Oct 2009 at 19:43:47, in comp.lang.javascript, Dr J R Stockton
wrote:
>In comp.lang.javascript message <4ac928fb$0$289$14726298(a)news.sunsite.dk
>>, Sun, 4 Oct 2009 23:00:03, FAQ server <javascript(a)dotinternet.be>
>posted:
>
>>FAQ Topic - How can I create a Date from a String?
>
>That makes no sense; should be "Date Object".
<snip>

Strictly speaking, the Date Object is the object named Date : the
constructor you use when you do new Date().

John
--
John Harris
From: Dr J R Stockton on
In comp.lang.javascript message <8CrRvmIqh3yKFwUx(a)J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Tue, 6 Oct 2009 18:03:38, John G Harris
<john(a)nospam.demon.co.uk> posted:
>On Mon, 5 Oct 2009 at 19:43:47, in comp.lang.javascript, Dr J R Stockton
>wrote:
>>In comp.lang.javascript message <4ac928fb$0$289$14726298(a)news.sunsite.dk
>>>, Sun, 4 Oct 2009 23:00:03, FAQ server <javascript(a)dotinternet.be>
>>posted:
>>
>>>FAQ Topic - How can I create a Date from a String?
>>
>>That makes no sense; should be "Date Object".
> <snip>
>
>Strictly speaking, the Date Object is the object named Date : the
>constructor you use when you do new Date().

The term is good enough for ISO/IEC 16262 15.9, which (for example)
includes "A Date object contains a number indicating a particular
instant in time to within a millisecond. The number may also
be NaN, indicating that the Date object does not represent a specific
instant of time.".

If you were quibbling over capitalisation, you should have done so
explicitly. Our Editor should be able to handle such details.

--
(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)
From: Hans-Georg Michna on
On Tue, 6 Oct 2009 20:12:45 +0100, Dr J R Stockton wrote:

>The term is good enough for ISO/IEC 16262 15.9, which (for example)
>includes "A Date object contains a number indicating a particular
>instant in time to within a millisecond. The number may also
>be NaN, indicating that the Date object does not represent a specific
>instant of time.".

While you're at it, I've seen scripts that compare two Date
objects directly, but I could not find any clue in any
specification that this should work.

The quoted text above also mentions the contained milliseconds,
but it does not say that the Date object behaves like a number.
It does not say how you get at those milliseconds. Elsewhere it
is specified that you can use .getTime() to get at them.

Hans-Georg
 |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11
Prev: Computer Techs Wanted
Next: move div by drag etc.