Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #18046 > unrolled thread

FAQ Topic - How can I create a Date object from a String? (2013-01-10)

Started by"FAQ server" <javascript@dotinternet.be>
First post2013-01-10 00:00 +0000
Last post2013-01-10 19:43 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  FAQ Topic - How can I create a Date object from a String? (2013-01-10) "FAQ server" <javascript@dotinternet.be> - 2013-01-10 00:00 +0000
    Re: FAQ Topic - How can I create a Date object from a String? (2013-01-10) Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-01-10 19:43 +0000

#18046 — FAQ Topic - How can I create a Date object from a String? (2013-01-10)

From"FAQ server" <javascript@dotinternet.be>
Date2013-01-10 00:00 +0000
SubjectFAQ Topic - How can I create a Date object from a String? (2013-01-10)
Message-ID<50ee0489$0$293$14726298@news.sunsite.dk>
-----------------------------------------------------------------------
FAQ Topic - How can I create a Date object 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);
}
}
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.

[toc] | [next] | [standalone]


#18059

FromDr J R Stockton <reply1300@merlyn.demon.co.uk.invalid>
Date2013-01-10 19:43 +0000
Message-ID<ujfdz1Ninx7QFwVd@invalid.uk.co.demon.merlyn.invalid>
In reply to#18046
In comp.lang.javascript message <50ee0489$0$293$14726298@news.sunsite.dk
>, Thu, 10 Jan 2013 00:00:03, FAQ server <javascript@dotinternet.be>
posted:

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

If the date string St is known to be valid YYYY-MM-DD,
        D= new Date(St.replace(/-/g, "/"))
will work.

-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web