Skip to content Skip to sidebar Skip to footer

Date.format In Chrome 5

I'm experiencing something really strange with my javascript in chrome with Date().format. If I use the console and type: d = new Date(Date.parse('2010-05-28')) d.format('yyyy-MM-d

Solution 1:

There is no format function in the Date object.

Are you getting that function from a plugin? If so then I suggest you read the APIs looking for the possible parameters.

I don't know if this might help, but all the plugins I tried required two "y" to get the four digit year string.

d = newDate(Date.parse("2010-05-28"))
d.format("yy-MM-dd");

EDIT: Some other plugin uses the uppercase "Y", like d.format("Y-m-d");

EDIT2: It seems like you need to just format your date to "yyyy-MM-dd". You can add this prototype function in your code:

Date.prototype.toMyPreferredFormat = function(){
  var dd=this.getDate();
  if(dd<10)dd='0'+dd;
  var mm=this.getMonth()+1;
  if(mm<10)mm='0'+mm;
  var yyyy=this.getFullYear();
  returnString(yyyy+"-"+mm+"-"+dd);
}

And use it like:

d = newDate(Date.parse("2010-05-28"))
d.toMyPreferredFormat();

Source of the prototype function

Post a Comment for "Date.format In Chrome 5"