How to sort by USA date (mm/dd/yyyy)?
How to sort by USA date (mm/dd/yyyy)?
dbaron
Posts: 10Questions: 4Answers: 0
This had been a question of 'how to sort by USA formated date data... but, I just found the answer... using columnDefs to define the type as date.
This can be closed.
Thanks!
-David
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
$(document).ready(function () {
var table = $('#gridProjectView').DataTable({
columnDefs: [
{ type: 'date-uk', targets: 3 } //specify your date column number,starting from 0
]
});
});
//If your date format is mm/dd//yyyy.
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"date-uk-pre": function (a) {
if (a == null || a == "") {
return 0;
}
var ukDatea = a.split('/');
return (ukDatea[2] + ukDatea[0] + ukDatea[1]) * 1;
},
"date-uk-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-uk-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
//OR
//If your date format is dd/mm/yyyy.
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"date-uk-pre": function (a) {
if (a == null || a == "") {
return 0;
}
var ukDatea = a.split('/');
return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
},
"date-uk-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-uk-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
Formatted date / time data is best sorted using the Moment.JS sorting plug-in for DataTables.
Allan