How to sort by USA date (mm/dd/yyyy)?

How to sort by USA date (mm/dd/yyyy)?

dbarondbaron Posts: 10Questions: 4Answers: 0
edited March 2017 in Free community support

This had been a question of 'how to sort by USA formated date data... but, I just found the answer... using columnDefs to define the type as date.

This can be closed.

Thanks!

-David

This question has an accepted answers - jump to answer

Answers

  • vishal.dholevishal.dhole Posts: 2Questions: 1Answers: 0
    edited March 2017

    $(document).ready(function () {
    var table = $('#gridProjectView').DataTable({
    columnDefs: [
    { type: 'date-uk', targets: 3 } //specify your date column number,starting from 0
    ]
    });
    });

    //If your date format is mm/dd//yyyy.
    jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "date-uk-pre": function (a) {
    if (a == null || a == "") {
    return 0;
    }
    var ukDatea = a.split('/');
    return (ukDatea[2] + ukDatea[0] + ukDatea[1]) * 1;
    },
    "date-uk-asc": function (a, b) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "date-uk-desc": function (a, b) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
    });

    //OR
    //If your date format is dd/mm/yyyy.
    jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "date-uk-pre": function (a) {
    if (a == null || a == "") {
    return 0;
    }
    var ukDatea = a.split('/');
    return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
    },
    "date-uk-asc": function (a, b) {
    return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "date-uk-desc": function (a, b) {
    return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
    });

  • allanallan Posts: 63,871Questions: 1Answers: 10,522 Site admin
    Answer ✓

    Formatted date / time data is best sorted using the Moment.JS sorting plug-in for DataTables.

    Allan

This discussion has been closed.