Problems with sorting by date (dd.mm.YYYY) in Data

2019-07-18 09:03发布

问题:

I'm trying to make a Datatable sortable using the method described in this blogpost. As described in the blogpost, I included the two files moment.min.js and datetime-moment.js into my page.

My dates are in the following format: 16.03.2015

Before initialising the datatable, I am executing moment, like:

$.fn.dataTable.moment('dd.mm.YYYY');

$('.datatable').DataTable({
    "retrieve": true,
    "bLengthChange": false,
    "autoWidth": true,
    "responsive": true,
    "oLanguage": {
    ......
    ......

Unfortunately, the table still gets sorted alphabetically when clicking on the date column. I already tried to change "DataTable" in "dataTable" and vice versa - but it doesn't make any difference! Mabye be I have to specify which column is the column with the date? But I could not find out how to specify it. On the Chrome Developer Console there is no error message or log. It just sorts the date column alphabetically all the time. I could use a different plugin - but the other one is declared as deprecated. Does anybody know what could be the problem that causes this plugin not to sort the datatable? Thank you!

回答1:

You're not using correct date format dd.mm.YYYY, see Moment.js documentation. dd is day of the week, and mm is minutes.

For dates in the format 16.03.2015 use the initialization code below:

$.fn.dataTable.moment('DD.MM.YYYY');