Date Sorting Issue

Date Sorting Issue

Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0

Link to test case:
https://jsfiddle.net/s6upbqn8/5/
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

In the Column tenth (Member Since) I am facing Sorting issue due to new year.

Can you plz check.

Answers

  • rf1234rf1234 Posts: 2,996Questions: 87Answers: 421

    You would probably need to use a plug in for date time sorting.
    This one is good:
    https://datatables.net/plug-ins/sorting/datetime-moment

    Here is a thread from SO with more information:
    https://stackoverflow.com/questions/53238318/html5-javascript-datatables-order-sort-of-columns/53238593#53238593

  • TruceTruce Posts: 1Questions: 0Answers: 0

    That thread is very old, dataTables supports date time sorting.

    $('#tblUsers').DataTable({
        "columnDefs": [
            { "targets": "sort-date", "type": "date" }
        ]
    });
    
    <th class="sorting sort-date" ... >Expiration Date</th>
    
  • rf1234rf1234 Posts: 2,996Questions: 87Answers: 421
    edited January 2023

    You are right. The thread is old and data tables has always been supporting date time sorting.

    The nice thing about the plug-in is that it auto-detects date columns. I never need your "target" definitions in "columDefs" and no modifications of my HTML either.

    Before I used the ultimate date plugin I had to do the same thing as you. For my dozens of data tables with hundreds of dateTime fields this meant a lot of code redundancy!

    https://datatables.net/plug-ins/sorting/datetime-moment

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    "type": "date"

    I'd really strongly suggest against doing that. If the auto-detection that @rf1234 mentions doesn't detect your data format, then forcing it to a date type could break things. Use the formatting options to define the formats yo have that you want to sort.

    Allan

Sign In or Register to comment.