jqGrid not sorting dates correctly

Gilbert Liddell picture Gilbert Liddell · May 3, 2011 · Viewed 11.6k times · Source

I have an issue when sorting dates using jqGrid. Given the following dates

jqGrid is sorting my dates as follows:

01/01/2010
01/01/2011
01/02/2010
01/02/2011

I would expect to see

01/01/2010
01/02/2010
01/01/2011
01/02/2011

My date format is dd/mm/yyyy and I have the follow format options -

{
    name: 'myDate',
    index: 'myDate',
    sortable: true,
    sorttype: 'date'
}

Have I missed something here or is this a limitation of the jqGrid?

Answer

Oleg picture Oleg · May 3, 2011

You made the typical error. The problem is that jqGrid expects the input of dates in the ISO 8601 date format: Y-m-d.

If you post the input data of jqGrid in the "dd/mm/yyyy" format you should add datefmt: 'm/d/Y' property to the column definition.

The best way would be to fill the date data of the grid in the ISO format and to convert the input in any other format only to display the date in jqGrid using the following settings

formatter:'date', formatoptions: {newformat:'m/d/Y'}, datefmt: 'd-M-Y'

Compare the source code of the tree demos: this, this and this.