I am using datatable plugin version 1.8.2 for displaying table on my webpage.
It is working fine except. It is not sorting dates properly, it is showing "invalid date" in Date object. below is my code snippet.
$(document).ready(function() {
jQuery.fn.dataTableExt.oSort['usdate-asc'] = function(a,b) {
/*
a and b are <div> tag with date
*/
var texta = ($(a).text()).toString(); // Here I am able to see my date in ' 03-17-2015 12:25:21 AM ' format
var textb = ($(b).text()).toString();// Here I am able to see my date in ' 03-17-2015 12:25:21 AM ' format
var usDatea = new Date(Date.parse(texta)); // Here it is showing "invalid date"
var usDateb = new Date(Date.parse(textb));
return ((usDatea < usDateb) ? -1 : ((usDatea > usDateb) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['usdate-desc'] = function(a,b) {
/*
a and b are <div> tag with date
*/
var texta = ($(a).text()).toString(); //same as above
var textb = ($(b).text()).toString(); //same as above
var usDatea = new Date(Date.parse(texta)); //same as above
var usDateb = new Date(Date.parse(textb)); //same as above
return ((usDatea < usDateb) ? 1 : ((usDatea > usDateb) ? -1 : 0));
};
$('#tablegridname').dataTable( {
"sPaginationType": 'full_numbers',
"bJQueryUI": true,
"iDisplayLength": 50,
"aLengthMenu":[50,100,500,1000],
"aaSorting": [[ 4, 'desc' ]],
"aoColumns": [null, null, null, null, {"sType": "usdate"}]
} );
});
});
Data.parse()not can recognise. BTW - you should really consider upgading your version of datatables to at least a version 1.9.4.03-17-2015 12:25:21 AMformat and I want to create a date object with this value so that I can compare it. how can I do it? with or without Date.parse()