JQuery dataTables sort doesn't work for dd-mm-yyyy format

In my project, I want to sort a date that is in dd-mm-yyyy format. I tried like this below

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "date-uk-pre": function(a) {
        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));
    }
});

      

But it doesn't work. Here, only the date and month are not sorted by year-end. I got from here link Sort date dd / mm / yyyy datatable date

+3


source to share


2 answers


I know this is an old question, but if you just came from Google there is now a built-in solution.

Just add an HTML5 attribute to the element:



<td data-th="Lastrun" data-order="[unixTimestamp]">
    [myWeirdDateFormat]
</td>

      

https://datatables.net/examples/advanced_init/html5-data-attributes.html

+9


source


Well it works for me out of the box, but I have less complicated date and time, so it's probably best if you use this http://datatables.net/plug-ins/sorting/



+1


source







All Articles