Sorting columns with date(Format : MM/dd/yyyy) with datatables not working

Sorting columns with date(Format : MM/dd/yyyy) with datatables not working

njgalnjgal Posts: 3Questions: 0Answers: 0
edited March 2012 in Plug-ins
Hi,
I am relatively new to datatables and I am kinds stuck with the sortiing of date fields with datatable. The dates I use are in the format MM/dd/yyyy. The datatable is interpreting the date as a String. So my dates are being displayed in the order
01/01/2011
01/01/2012
02/01/2011

This is my code where I am initializing the dataatble:

$(document).ready(function() {
$('#mydatatable').dataTable({
"bDestroy" : true,
"aoColumns": [
{ "sType": "us_date" },
null,
null,
null,
null,
null,
null,
null,
null,
null
],
"oLanguage": {
"sLengthMenu": "Display _MENU_ records per page",
"sInfo": "Showing _START_ to _END_ of _TOTAL_ records",
"sInfoEmpty": "Showing 0 to 0 of 0 records",
"sInfoFiltered": "(filtered from _MAX_ total records)",
"sSearch": "Search:"
},

} );
} );

And here is the definition for us-date
"us_date-asc" : function(a,b) {
var ukDatea = a.split('/');
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[0] + ukDatea[1]) * 1;
var y = (ukDateb[2] + ukDateb[0] + ukDateb[1]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};

"us_date-desc" : function(a,b) {
var usDatea = a.split('/');
var usDateb = b.split('/');
var x = (usDatea[2] + usDatea[0] + usDatea[1]) * 1;
var y = (usDateb[2] + usDateb[0] + usDateb[1]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}

Replies

  • njgalnjgal Posts: 3Questions: 0Answers: 0
    Please could anybody help me out with this ?
  • stonefieldstonefield Posts: 2Questions: 0Answers: 0
    You mistake in wrong valulues. Try this var x = usDatea[2]*365 + usDatea[0]*30 + usDatea[1];
    var y = usDatea[2]*365 + usDatea[0]*30 + usDatea[1];
This discussion has been closed.