Date (MMM yyyy) or (MMMM yyyy)

  • Author: Phil Hurwitz
  • Deprecated: This plug-in has been deprecated and replaced with other functionality. Please see the detailed description below for more information.

This sorting plug-in will sort, in calendar order, data which is in the format "MMM yyyy" or "MMMM yyyy". Inspired by forum discussion: http://datatables.net/forums/discussion/1242/sorting-dates-with-only-month-and-year

Please note that this plug-in is **deprecated*. The datetime plug-in provides enhanced functionality and flexibility.

Plug-in code

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "stringMonthYear-pre": function (s) {
        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

        var dateComponents = s.split(" ");
        dateComponents[0] = dateComponents[0].replace(",", "");
        dateComponents[1] = jQuery.trim(dateComponents[1]);

        var year = dateComponents[1];

        var month = 0;
        for (var i = 0; i < months.length; i++) {
            if (months[i].toLowerCase() == dateComponents[0].toLowerCase().substring(0,3)) {
                month = i;
                break;
            }
        }

        return new Date(year, month, 1);
    },

    "stringMonthYear-asc": function (a, b) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "stringMonthYear-desc": function (a, b) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
});

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$('#example').DataTable( {
     columnDefs: [
       { type: 'stringMonthYear', targets: 0 }
     ]
  } );