View Full Version : Sort Date Grid

28 Oct 2011, 11:29 AM
Hey there everyone,
I am having a problem with sorting a column of type 'date' on my grid. I am getting a JSON object in dateFormat 'U' (e.g. '1302539400000') and I am trying to render this in a 'm-d-Y g:i:s' format using a convert function in 'datetime' model field. I am able to get this to render correctly but I am unable to sort this column.

Here is the field in my model.

fields : [
name : 'datetime',
type: 'date',
dateFormat: 'm-d-Y g:i:s',
convert : function(v, rec) {
var dt = new Date(v);
return Ext.Date.format(dt, 'm-d-Y g:i:s');

and here is the column in my grid.

header: 'Time',
dataIndex: 'datetime',
width: 135,
menuDisabled: true,
sortable: true

I am probably missing something small, but with all the tweaking I have done I have yet to find a solution. Sorry if there has already been a post about this, but so far I haven't found a fix. Any help is appreciated. Thanks!

28 Oct 2011, 11:46 AM
Try this:

Get rid of the convert function on the field.
Set the field's dateFormat to 'U'. The field will now contain Date object and it knows how to sort them.
Switch the column to xtype: 'datecolumn'.
Set the column's format to 'm-d-Y g:i:s'. This will render the Date objects correctly in the column.

28 Oct 2011, 12:16 PM
Thanks skirtle!
I can now sort the date column. Unfortunately I'm getting some weird dates now. When I put the epoch time into a conversion website it returns the correct date Mon Apr 11 2011 09:30:00 GMT-0700 (Pacific Daylight Time), but in my extjs grid it returns as Mon Oct 30 43245 05:00:00 GMT-0700 (Pacific Daylight Time). Is 'U' the correct format to use, or is there some additional formatting I need to look at?

EDIT: I think my JSON object is sending back epoch time in milliseconds, not seconds. To fix this I just changed my dateFormat from 'U' to 'time'.