Date Sort issue Not solved with moment js

Date Sort issue Not solved with moment js

meghatpmeghatp Posts: 1Questions: 1Answers: 0

I am quite not sure where it went wrong. Still its sorting as string
My demo application not working as expected.

<head runat="server">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.19/sorting/datetime-moment.js"></script>


<script type="text/javascript">

    $(document).ready(function () {
         $.fn.dataTable.moment( 'DD-MM-YYYY' );
        $('.testtable').dataTable();
    });
</script>
<title></title>

</head>
<body>

<table class="testtable">
    <thead>
        <tr>
            <th>Date</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>21/11/2001</td>
        </tr>
        <tr>
            <td>2/12/2001</td>
        </tr>
        <tr>
            <td>1/12/2001</td>
        </tr>
        <tr>
            <td>1/6/2001</td>
        </tr>
    </tbody>
</table>

</body>
</html>

Answers

  • kthorngrenkthorngren Posts: 20,649Questions: 26Answers: 4,836

    The problem is the $.fn.dataTable.moment( 'DD-MM-YYYY' ); for doesn't match your format in the table. You have <td>1/6/2001</td>. I think your format should be$.fn.dataTable.moment( 'D/M/YYYY' );`. You have single digit day and month along with slashes (/). Here is the moment.js formatting table:
    https://momentjs.com/docs/#/displaying/format/

    Kevin

This discussion has been closed.