From: FAQ server on
-----------------------------------------------------------------------
FAQ Topic - How do I format a Date object with javascript?
-----------------------------------------------------------------------

A local `Date` object where `0 <= year <= 9999` can be
formatted to a common ISO 8601 format `YYYY-MM-DD` with:-

/** Formats a Date to YYYY-MM-DD (local time), compatible with both
* ISO 8601 and ISO/IEC 9075-2:2003 (E) (SQL 'date' type).
* @param {Date} dateInRange year 0000 to 9999.
* @throws {RangeError} if the year is not in range
*/
function formatDate(dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = year >= 0 && year <= 9999, yyyy, mm, dd;
if(!isInRange) {
throw RangeError("formatDate: year must be 0000-9999");
}
yyyy = ("000" + year).slice(-4);
mm = ("0" + (dateInRange.getMonth() + 1)).slice(-2);
dd = ("0" + (dateInRange.getDate())).slice(-2);
return yyyy + "-" + mm + "-" + dd;
}

<URL: http://www.merlyn.demon.co.uk/js-date9.htm>


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: Asen Bozhilov on
FAQ wrote:
> -----------------------------------------------------------------------
> FAQ Topic - How do I format a Date object with javascript?
> -----------------------------------------------------------------------

Another possible approach is:

function formatDate(dateInRange) {
var year = dateInRange.getFullYear(),
isInRange = (year >= 0 && year <= 9999),
yyyy, mm, dd;

if(!isInRange) {
throw RangeError("formatDate: year must be 0000-9999");
}

yyyy = year + 10000;
mm = (dateInRange.getMonth() + 1) + 100;
dd = dateInRange.getDate() + 100;
return (yyyy + "-" + mm + "-" + dd).replace(/\b1/g, '');
}

From: Andrea Giammarchi on
Just in case one day other vendors will implement the same ...

function formatDate(date) {
var year = date.getFullYear();
if (year < 1 || 9999 < year) {
throw RangeError("formatDate: year must be 0000-9999");
}
// Mozilla guys rock!
return date.toLocaleFormat("%Y-%m-%d");
}

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/toLocaleFormat

Regards,
Andrea Giammarchi
From: dhtml on
c
From: dhtml on
On Jul 30, 7:45 am, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
> FAQ wrote:
> > -----------------------------------------------------------------------
> > FAQ Topic - How do I format a Date object with javascript?
> > -----------------------------------------------------------------------
>
> Another possible approach is:
>
> function formatDate(dateInRange) {
> var year = dateInRange.getFullYear(),
> isInRange = (year >= 0 && year <= 9999),
> yyyy, mm, dd;
>
> if(!isInRange) {
> throw RangeError("formatDate: year must be 0000-9999");
> }
>
> yyyy = year + 10000;
> mm = (dateInRange.getMonth() + 1) + 100;
> dd = dateInRange.getDate() + 100;
> return (yyyy + "-" + mm + "-" + dd).replace(/\b1/g, '');
>
>

It's fine, but why do you think this is better or do you think the FAQ
should use this instead?

I've not been posting because my primary machine is down and don't
have newsreader setup on this machine. I do not like GG and I think I
may have just posted "c" by accident.

I am aware of the FAQ entry for "how do I format a number" and the
proposal on how to read and post (using a newsreader). I will get to
those. Plus the JSON stuff, which takes a bit more time.

Should be on it by Thursday.
--
Garrett