From: FAQ server on 25 Jul 2010 19:00 ----------------------------------------------------------------------- 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 30 Jul 2010 10:45 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 31 Jul 2010 19:29 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 3 Aug 2010 04:10 c
From: dhtml on 3 Aug 2010 04:12 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
|
Next
|
Last
Pages: 1 2 Prev: FAQ Topic - What does the future hold for ECMAScript? (2010-07-20) Next: trying to do AJAX |