Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > FAQ Topic - How can I create a Date from a String? (2009-10-05)

Reply
Thread Tools

FAQ Topic - How can I create a Date from a String? (2009-10-05)

 
 
FAQ server
Guest
Posts: n/a
 
      10-04-2009
-----------------------------------------------------------------------
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.

 
Reply With Quote
 
 
 
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-05-2009
In comp.lang.javascript message <4ac928fb$0$289$
>, Sun, 4 Oct 2009 23:00:03, FAQ server <>

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.
 
Reply With Quote
 
 
 
 
John G Harris
Guest
Posts: n/a
 
      10-06-2009
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$
>>, Sun, 4 Oct 2009 23:00:03, FAQ server <>

>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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-06-2009
In comp.lang.javascript message <830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Tue, 6 Oct 2009 18:03:38, John G Harris
<> 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$
>>>, Sun, 4 Oct 2009 23:00:03, FAQ server <>

>>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)
 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      10-07-2009
On Tue, 6 Oct 2009 at 20:12:45, in comp.lang.javascript, Dr J R Stockton
wrote:
>In comp.lang.javascript message <830F0FF37FB96852AD0
>8924D9443D28E23ED5CD>, Tue, 6 Oct 2009 18:03:38, John G Harris
><> posted:


<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.

<snip>

In any other OO language you would say that Date is the function object
and a Date instance is an object constructed by Date. 'instance' should
be used more often in javascript discussions.

In the standard, "Date object" means the function object in section 4.2
and an object constructed by Date in section 15.9. The term is
thoroughly ambiguous and should be avoided where clarity is desired.

John
--
John Harris
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-07-2009
John G Harris wrote:

> Dr J R Stockton wrote:
>> John G Harris posted:
>>> 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.

> <snip>
>
> In any other OO language you would say that Date is the function object
> and a Date instance is an object constructed by Date.


It is also used in the ECMAScript Language Specification(s) on numerous
occasions.

> 'instance' should be used more often in javascript discussions.


My words exactly.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$>
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-07-2009
In comp.lang.javascript message <lghpc5dq02b5vs17m33dn6vlo85vjpq8rt@4ax.
com>, Wed, 7 Oct 2009 18:49:45, Hans-Georg Michna <hans-
> posted:
>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.



To most people, I guess it's not obvious what D1 == D2 does; I believe
it compares the objects to see if they are the same object. One might
think it could compare the toString() results, but that would be
wasteful of effort. Just compare +D1 with +D2.

You could have read <URL:http://www.merlyn.demon.co.uk/js=-date0.htm#DC>
instead of asking.


>The quoted text above also mentions the contained milliseconds,
>but it does not say that the Date object behaves like a number.


I only quoted the part of the document relevant to previous discussion;
if you want to know more, you should read it.

>It does not say how you get at those milliseconds. Elsewhere it
>is specified that you can use .getTime() to get at them.


Or .valueOf() - and if you consider the whole document you will see that
a unary + operator should do it (it does, and unary - does, and D-0
works, as does multiplying or dividing by 1, or using Number(D) ).


AFAICS, a Date object is not required to store its value as an IEEE
Double; but it is required to behave as if that were the case (apart
from the speed of operations, which is unspecified). There might be
advantage in actually storing a 64-bit signed integer.

--
(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.
 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      10-07-2009
"FAQ server" wrote:

> function parseISO8601(dateStringInRange)


parseISO8601 and Date object in ECMA 3 works with dates from Gregorian
calendar. And they follows algorithm for leap year in Gregorian
calendar, where:

year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)

In Julian calendar leap year is every year who's:

year % 4 == 0

Gregorian calendar since from 24 February 1582.
<URL:http://en.wikipedia.org/wiki/Gregorian_calendar>

Because that, date likes this:

new Date(1300, 1, 29); // Mon Mar 01 1300 00:00:00 GMT+0200 (FLE
Standard Time)
new Date(1400, 1, 29); // Sat Mar 01 1400 00:00:00 GMT+0200 (FLE
Standard Time)
new Date(1500, 1, 29); // Thu Mar 01 1500 00:00:00 GMT+0200 (FLE
Standard Time)

For my that's dates is absolutely valid date. That's dates existed in
real life.

 
Reply With Quote
 
Garrett Smith
Guest
Posts: n/a
 
      10-07-2009
John G Harris wrote:
> 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$
>>> , Sun, 4 Oct 2009 23:00:03, FAQ server <>

>> 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().
>


And one of those is "a Date".

A Date *is* and Object. Adding "Object" is redundant.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      10-08-2009
In comp.lang.javascript message <jNkCdHLxTOzKFwP$@J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Wed, 7 Oct 2009 19:58:57, John G Harris
<> posted:

>In the standard, "Date object" means the function object in section 4.2
>and an object constructed by Date in section 15.9. The term is
>thoroughly ambiguous and should be avoided where clarity is desired.


You'll tell that to TC39, I trust?

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk BP7, Delphi 3 & 2006.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
NOT <URL:http://support.codegear.com/newsgroups/>: news:borland.* Guidelines
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
FAQ Topic - How can I create a Date object from a String? (2011-02-15) FAQ server Javascript 62 03-04-2011 10:47 PM
FAQ Topic - How can I create a Date from a String? (2009-12-02) FAQ server Javascript 0 12-02-2009 12:00 AM
FAQ Topic - How can I create a Date from a String? (2009-08-09) FAQ server Javascript 1 08-10-2009 10:38 PM
FAQ Topic - What questions are on-topic for CLJ? (2008-10-16) FAQ server Javascript 1 10-16-2008 12:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57