How to sort the datetime field in datatable plugin

2019-09-07 09:36发布

问题:

I'm used to sort the date and time field in datatable plugin.but it not working it sort in the order of 9,8,7,...12,11,10. If I'm use some custom code for sorting I works but it is not sorting for some data's

Custom code:

HTML and JS

<td><?=  $date ? $date : '-' ?></td>

jQuery.fn.dataTableExt.oSort['uk_date-pre'] = function(a) {
var a = a.split('m')[0];
a=a+'m';
a = a.slice(0, -2) + ' ' + a.slice(-2);
var date = Date.parse(a);
return typeof date === 'number' ? date : -1;
}

datatable:

$('#id').DataTable({
    "paging": true,
    "ordering": true,
    "aoColumns": [
       { "bSortable": false },  
       { sType: 'uk_date' },
       null
    ],
    "order": [[0, 'desc']],
});

回答1:

Finally i got the answer thanks you for all for your comments

jQuery.fn.dataTableExt.oSort['uk_date-pre'] = function(a) {
var a = a.split('m')[0];
if(a == '-'){
        return -1;
    }
a=a+'m';
a = a.slice(0, -2) + ' ' + a.slice(-2);
var date = Date.parse(a);
return typeof date === 'number' ? date : -1;
}