PDA

View Full Version : Converting a date string in a model field, used in a grid



raskri
16 Dec 2011, 1:40 PM
I load a string representing a date: 201112162230(Ymdhi).~o)

I want it to look like this: 22:30 16/12 2011(h:i d/m Y).

Setting the dateFormat config, in the field defenition, to "Ymdhi" just gives me something like this in the grid:
Fri Dec 16 2011 22:30:00 GMT+0100 (Romance Standard Time)

How can I convert this, without using my home made convert function written below?


{
name: 'date',
type: 'date',
convert: function(value){
var year = value.substr(0,4);
var month = value.substr(4,2);
var day = value.substr(6,2);
var hour = value.substr(8,2);
var min = value.substr(10,2);
return hour + ':' + min + ' ' + day + '/' + month + ' ' + year;
}
}

mitchellsimoens
17 Dec 2011, 6:53 AM
I would set your field up like this:


{
name : 'date',
type : 'date',
dateFormat : 'Ymdhi'
}

That should parse the string based on the dateFormat for you instead of you doing it manually in the convert method.

Now in your grid column, have a renderer like this:


{
header : 'Date',
dataIndex : 'date',
width : 100, //or flex
renderer : Ext.util.Format.dateRenderer('h:i d/m Y')
}

For each record, the renderer will get fired and dateRenderer returns a function to be used based on the date format string you pass it.