Sorting Oracle Dates using Tablesorter

Lately, I’ve been using the Tablesorter jQuery plugin to add sort & pagination functions to tables in my applications. The plugin works great, but by default it doesn’t appear to work correctly with Oracle formatted dates, e.g. 05-JUL-13.

I found a similar question on Stackoverflow, which I have adapted to work for my applications.

To enable sorting of Oracle dates, add the following to your application:

ts.addParser({
        id: "oracleDate",
        is: function (s) {
            return /^\d{1,2}[\/\-]\w{3}[\/\-]\d{2}$/.test(s);
        }, format: function(s) {

            var monthNames = {};
            monthNames["JAN"] = "01";
            monthNames["FEB"] = "02";
            monthNames["MAR"] = "03";
            monthNames["APR"] = "04";
            monthNames["MAY"] = "05";
            monthNames["JUN"] = "06";
            monthNames["JUL"] = "07";
            monthNames["AUG"] = "08";
            monthNames["SEP"] = "09";
            monthNames["OCT"] = "10";
            monthNames["NOV"] = "11";
            monthNames["DEC"] = "12";

            if (s == "") return 0;

            var date = s.match(/^(\d{1,2})[\/\-](\w{3})[\/\-](\d{2})$/);
            var day = String(date[1]);
            if (day.length == 1) { day = "0" + day;}
            var month = monthNames[date[2]];
            var year = date[3];

            return sortableDate = '' + year + month + day;
        },
        type: 'numeric'
    });
Advertisements