Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   FAQ Topic - How do I format a Date object with javascript? (2010-07-26) (http://www.velocityreviews.com/forums/t941738-faq-topic-how-do-i-format-a-date-object-with-javascript-2010-07-26-a.html)

FAQ server 07-25-2010 11:00 PM

FAQ Topic - How do I format a Date object with javascript? (2010-07-26)
 
-----------------------------------------------------------------------
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.


Asen Bozhilov 07-30-2010 02:45 PM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
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, '');
}


Andrea Giammarchi 07-31-2010 11:29 PM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
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/Cor...toLocaleFormat

Regards,
Andrea Giammarchi

dhtml 08-03-2010 08:10 AM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
c

dhtml 08-03-2010 08:12 AM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
On Jul 30, 7:45 am, Asen Bozhilov <asen.bozhi...@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

dhtml 08-03-2010 08:13 AM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
On Aug 3, 1:12*am, dhtml <dhtmlkitc...@gmail.com> wrote:
> On Jul 30, 7:45 am, Asen Bozhilov <asen.bozhi...@gmail.com> wrote:
>

[...]
> have newsreader setup on this machine. I do not like GG and I think I
> may have just posted "c" by accident.


Confirmed.
--
Garrett


Asen Bozhilov 08-03-2010 09:00 AM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
dhtml wrote:

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


Both versions are good. I posted my code, just for give another
alternative for people who read the FAQ entries. I am not pretended
for my version is better than FAQ code. In my version I do not use
three time string concatenations plus three invocation of `slice'
method. I am just using mathematical operation plus `replace' which I
suppose is faster than than version used by FAQ.

Lasse Reichstein Nielsen 08-04-2010 04:16 PM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
dhtml <dhtmlkitchen@gmail.com> writes:

> On Jul 30, 7:45 am, Asen Bozhilov <asen.bozhi...@gmail.com> wrote:


>> return (yyyy + "-" + mm + "-" + dd).replace(/\b1/g, '');


I think RegExp replace is a little heavy-duty for something like this.
How about
return String(yyyy).substring(1) + "-" +
String(mm).substring(1) + "-" +
String(dd).substring(1);
?

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'


Dr J R Stockton 08-04-2010 07:41 PM

Re: FAQ Topic - How do I format a Date object with javascript? (2010-07-26)
 
In comp.lang.javascript message <0df43f3b-dfc9-41a7-9e42-bf85f31ff2f6@t2
0g2000yqa.googlegroups.com>, Tue, 3 Aug 2010 02:00:48, Asen Bozhilov
<asen.bozhilov@gmail.com> posted:

> I am just using mathematical operation plus `replace' which I
>suppose is faster than than version used by FAQ.



You can easily test that using a * * COPY * * of
<URL:http://www.merlyn.demon.co.uk/js-quick.htm>. 'Press' Demo 6 times,
edit in your functions and their calls, press Eval. Repeat increasing
K_ and pressing Eval until you get reasonable times below the buttons.

In Firefox 3.0.19, WinXP sp3 P4/3GHz, K_ = 15000, I get 0,11,140,276
and similar - the FAQ is a little over twice as fast. Chrome 5.0,
0,10,34,59. Safari 5.0, 0,1,37,59. Opera 10.10, 0,0,140,422. But IE8,
0,0,344,297 - perhaps MS are comparatively good at RegExps.

The FAQ does need good, realistic examples of RegExps, though.

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

Asen Bozhilov 08-05-2010 08:54 AM

Re: FAQ Topic - How do I format a Date object with javascript?(2010-07-26)
 
Lasse Reichstein Nielsen wrote:
> I think RegExp replace is a little heavy-duty for something like this.


Indeed. I had to test in various implementations. I tested my approach
in Rhino 1.7 under Debian 5.0 and was faster than FAQ. From tests
provided by Dr J R Stockton and mines FAQ approach is faster than my.
Do you have explanation why? I am still thinking RegExp approach
should be faster than three string concatenation + three invocations
of `slice'.

> How about
> *return String(yyyy).substring(1) + "-" +
> * * *String(mm).substring(1) + "-" +
> * * *String(dd).substring(1);
> ?


Btw, I used similar version before I post RegExp approach. Do you have
any reasons to use `substring' instead of `slice'?



All times are GMT. The time now is 07:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.