Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery dataTables sorting is not working for dd-mm-yyyy format

In my project I want to sort date which 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 this is not working. Here only date and month is getting sorted not also on the basis of year. I took reference from here Datatable date sorting dd/mm/yyyy issue

like image 360
Anju Avatar asked Dec 12 '22 02:12

Anju


2 Answers

I know this is an old question, but in case you've just come here from Google, there is a built in solution now.

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

like image 174
markBlack Avatar answered May 16 '23 08:05

markBlack


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

like image 44
Rohit Hazra Avatar answered May 16 '23 07:05

Rohit Hazra