You must Sign In to post a response.
  • Category: ASP.Net MVC

    Date Sorting issue by using JQuery datatable()

    Hi.
    I have one view where I display data using jquery datatable.
    After binding, I have added below code to add jquery class "datatable()" to table.

    <script>
    $(document).ready(function () {
    $('#tblDocuments').DataTable({
    "paging": true,
    "pageLength": 5,
    "ordering": true,
    "info": false,
    "bFilter": false
    });
    });
    </script>

    Here, In my table, column contains date value like, "15-09-2016".
    While sorting, it considers only date. Sorting not work for month and year.
    How can I do that.
  • #768263
    Correction.

    Sorting considers this value like a string not even date.

  • #768273
    You need to use $.fn.dataTable.moment( 'DD-MM-YYYY HH:mm:ss' ); in you code for fie Date Sorting issue by using JQuery datatable. Here is code snippet as guideline
    "date-asc": function ( a, b )
    {
    alert("dateasc")
    var x = Date.parse( a );
    var y = Date.parse( b );

    if ( isNaN(x) || x==="" )
    {
    x = Date.parse( "01/01/1970 00:00:00" );
    }
    if ( isNaN(y) || y==="" )
    {
    y = Date.parse( "01/01/1970 00:00:00" );
    }

    return x - y;
    },

    "date-desc": function ( a, b )
    {
    var x = Date.parse( a );
    var y = Date.parse( b );

    if ( isNaN(x) || x==="" )
    {
    x = Date.parse( "01/01/1970 00:00:00" );
    }
    if ( isNaN(y) || y==="" )
    {
    y = Date.parse( "01/01/1970 00:00:00" );
    }

    return y - x;
    }


Sign In to post your comments