jqGrid Doesn't Sort When Showing Epoch Time (since as milliseconds) as Date

I use jqGrid and my grid definition is like that:

...
colNames:['Type','Date','Message','User Name','Host'],
colModel:[{name:'type',index:'type', width:100},
{name:'date',index:'date', sorttype:'date', formatter:'date', 
  formatoptions: {newformat:'d-M-Y'}, width:100},
{name:'log',index:'log', width:200},
{name:'username',index:'username', width:50},
{name:'host',index:'host', width:50}], 
...

When I debug my coming data one of the date value (it is Number) is as follows:

1322550786997

Grid shows it like that:

29-Nov-2011

Everything is OK till this point. However when I want to sort my date column it doesn't change anything.

Any ideas?

Answers


The problem is that decoding of the Unix (formatoptions: {srcformat: 'U', newformat: 'd-M-Y'}) date 1322550786997 get us 19-Dec-43879 and not 29-Nov-2011. You correct representation of the date will be the string "\/Date(1322550786997)\/" instead of the number 1322550786997.

See the demo:

UPDATED: You can also use the following custom formatter as a workaround

formatter: function (cellval, opts) {
    var date = new Date(cellval);
    opts = $.extend({}, $.jgrid.formatter.date, opts);
    return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts);
}

It creates Date and then use original date formatter to convert it to the format 'd-M-Y'. See here the demo.


Need Your Help

Change nav a.class on horizontal scroll with jQuery

jquery html css scroll addclass

I'm working with a horizontal design, with a navigation menu in the top left corner.

How to make sqllite database call and store return value to variable

javascript jquery sqlite

I need to covert C# code to JavaScript and I've got stuck with async database calls.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.