PDA

View Full Version : EXT: Ext.util.Format.dateRenderer giving previous date



govindaswamym
2 Jul 2013, 7:16 PM
Hi,

In my code I am using Ext.util.Format.dateRenderer to format my date.
Ext.util.Format.dateRenderer('m/d/Y')("2013-07-22")
Expected Output : "07/22/2013"
Actual Output : "07/21/2013"

Is there anyway I can get correct date with my required format? I tried Ext.util.date and it also gives the same result. I am using ExtJS 4.1.

Thanks,
Govind

carol.ext
3 Jul 2013, 8:06 AM
Ext.util.Format.dateRenderer returns a function that uses UtilFormat.date(v, format);

UtilFormat.date uses Date.parse() (see code below : v = new Date(Date.parse(v)); )


Looks like the date you are passing in is parsed as UTC rather than local time by Date.parse(), that is how it ends up as the day before. In some browsers it is not parsed at all! Passing in dates with dashes instead of slashes is pretty unpredictable.

Discussion about that at these links :

Stack overflow discussion on topic (http://stackoverflow.com/questions/2587345/javascript-date-parse)

Javascript and dates - what a mess blog post (http://blog.dygraphs.com/2012/03/javascript-and-dates-what-mess.html)

dateRenderer :



dateRenderer : function(format) {
return function(v) {
return UtilFormat.date(v, format);
};
},


UtilFormat.date :


date: function(v, format) {
if (!v) {
return "";
}
if (!Ext.isDate(v)) {
v = new Date(Date.parse(v));
}
return Ext.Date.dateFormat(v, format || Ext.Date.defaultFormat);
},



IE9 for me does not parse that date at all :
Date.parse("2013-07-22","Y-m-d")
NaN

droessner
3 Jul 2013, 8:59 AM
Hi,

In my code I am using Ext.util.Format.dateRenderer to format my date.
Ext.util.Format.dateRenderer('m/d/Y')("2013-07-22")
Expected Output : "07/22/2013"
Actual Output : "07/21/2013"

Is there anyway I can get correct date with my required format? I tried Ext.util.date and it also gives the same result. I am using ExtJS 4.1.

Thanks,
Govind

I'm not sure exactly what you are using this for, but if you are just trying to re-format your date in a different way, you could just use the parse method in Ext.Date to parse your string as a date and then the format method to format it the way that you want. Something like this:

Ext.Date.format(Ext.Date.parse('2013-07-22', 'Y-m-d'), 'm/d/Y')

govindaswamym
3 Jul 2013, 11:49 AM
Thanks a lot for the info..