views:

75

answers:

4

Hi, all.

I'm trying to use JS to turn a date object into a String in YYYMMDD format. Is there an easier way than concatenating Date.getYear() Date.getMonth(), and Date.getDay()?

Thanks,
IVR Avenger

A: 

No, this is by far the easiest and the fastest.

Babiker
A: 

This guy here => http://blog.stevenlevithan.com/archives/date-time-format wrote a format() function for the Javascript's Date object, so it can be used with familiar literal formats.

If you need full featured Date formatting in your app's Javascript, use it. Otherwise if what you want to do is a one off, then concatenating getYear(), getMonth(), getDay() is probably easiest.

Strelok
A: 

It seems that mootools provide Date().format() : http://mootools.net/docs/more/Native/Date

I'm not sure if it worth including just for this particular task though.

Mar_Garina
A: 

Altered piece of code I often use:

 Date.prototype.yyyymmdd = function() {
   var yyyy = this.getFullYear().toString();
   var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
   var dd  = this.getDate().toString();
   return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); // padding
  };

d = new Date();
d.yyyymmdd();
paque