How to sort table by dd/mm/yyyy

How to sort table by dd/mm/yyyy

davsevdavsev Posts: 5Questions: 3Answers: 0
edited March 2017 in Free community support

How do I sort table by dd/mm/yyyy format?
This is my code.

$(document).ready(function(){ $.fn.dataTable.moment( 'D/M/YYYY' ); $('#leadtable').dataTable({ language: { url: '//cdn.datatables.net/plug-ins/1.10.12/i18n/Hebrew.json', } }); });

I added the moment cdn at the head right after the datatable files like this

!-- datatable -->
<link href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css" rel="stylesheet">



Searched around the site a bit but couldent find a working answer.
Sort works if the date format is yyyy.mm.dd
But it is not the required date format i need.

This is what i get now

any help would be great
tnx!

Answers

  • allanallan Posts: 60,986Questions: 1Answers: 9,944 Site admin

    Thanks for your question - however, per the forum rules can you link to a test case showing the issue please. This will allow the issue to be debugged.

    Information on how to create a test page, if you can't provide a link to your own page can be found here.

    Thanks,
    Allan

  • davsevdavsev Posts: 5Questions: 3Answers: 0
    edited March 2017

    Thank you for the suggestion.
    I have changed my code.
    This is a fiddle of my table:

    https://jsfiddle.net/davseveloff/v7oozsLv/2/

    As you can see the sorting is not working very good.
    At my website it is even worst. the sorting sorts firs the day (dd) than the month(mm) and finally the year(yyyy) for example 02/10/2015 wll appear before 10/2/2017.

    As well I have a double arrow at the sort <th> that does not appear in the fiddle, What can couse it?

  • allanallan Posts: 60,986Questions: 1Answers: 9,944 Site admin

    You have non-date data in the date column, which means that DataTables cannot correctly detect the date and thus is treating it as a string.

    You would need to have a custom ordering plug-in that would strip out all the non-date information and just leave the date value.

    Allan

  • davsevdavsev Posts: 5Questions: 3Answers: 0
    edited March 2017

    Hey,
    Thank you for your fast answer!

    I worked it out

    $(document).ready(function() {
       jQuery.extend(jQuery.fn.dataTableExt.oSort, {
        "extract-date-pre": function(value) {
            var date = $(value, 'span')[0].innerHTML;
            date = date.split('/');
            return Date.parse(date[1] + '/' + date[0] + '/' + date[2])
        },
        "extract-date-asc": function(a, b) {
            return ((a < b) ? -1 : ((a > b) ? 1 : 0));
        },
        "extract-date-desc": function(a, b) {
            return ((a < b) ? 1 : ((a > b) ? -1 : 0));
        }
    });
    $('#leadtable').dataTable({
        language: {
            url: '//cdn.datatables.net/plug-ins/1.10.12/i18n/Hebrew.json'
        },
        columnDefs: [{
                type: 'extract-date',
                targets: [0]
            }
    
        ]
    });
    
  • allanallan Posts: 60,986Questions: 1Answers: 9,944 Site admin

    Nice one! Thanks for posting back with your solution.

    Allan

This discussion has been closed.