Expanding on Shaun's fine answer, there are at least two ways to go about testing the need to shim for dd-MMM-yyyy format. I've modified Shaun's code after intense consultation with JSLint (I swear it hates every line of ECMA I write).
Using IE conditionals
If you're already using IE conditionals (<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
), then you can just test for HTML.lt-ie9
and conditionally define the new sorting algorithm, then call dataTables:
//test for html.lt-ie9
if ($('html.lt-ie9').length) {
//create the new magic sorting
var customDateDDMMMYYYYToOrd = function (date) {
"use strict"; //let's avoid tom-foolery in this function
// Convert to a number YYYYMMDD which we can use to order
var dateParts = date.split(/-/);
return (dateParts[2] * 10000) + ($.inArray(dateParts[1].toUpperCase(), ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]) * 100) + dateParts[0];
};
// This will help DataTables magic detect the "dd-MMM-yyyy" format; Unshift so that it's the first data type (so it takes priority over existing)
jQuery.fn.dataTableExt.aTypes.unshift(
function (sData) {
"use strict"; //let's avoid tom-foolery in this function
if (/^([0-2]?\d|3[0-1])-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}/i.test(sData)) {
return 'custom-date-dd-mmm-yyyy';
}
return null;
}
);
// define the sorts
jQuery.fn.dataTableExt.oSort['custom-date-dd-mmm-yyyy-asc'] = function (a, b) {
"use strict"; //let's avoid tom-foolery in this function
var ordA = customDateDDMMMYYYYToOrd(a),
ordB = customDateDDMMMYYYYToOrd(b);
return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
};
jQuery.fn.dataTableExt.oSort['custom-date-dd-mmm-yyyy-desc'] = function (a, b) {
"use strict"; //let's avoid tom-foolery in this function
var ordA = customDateDDMMMYYYYToOrd(a),
ordB = customDateDDMMMYYYYToOrd(b);
return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
};
};
//now call the dataTable plugin against the target tables (in this case, any table with `class="dataTable"`)
$('table.datatable').dataTable();
See the IE conditionals example at http://jsfiddle.net/jhfrench/nEsCt/
Testing with Modernizr
On the other hand, if you're inclined to use Modernizr to test for the capability, we can define the Modernizr test, then use Modernizr to execute the test and conditionally load the shim magic (from a .js file), then call dataTables:
//define the Modernizr test
Modernizr.addTest('valid_date_dd_mmm_yyyy', function() {
return !isNaN(Date.parse("17-MAY-2013"));
});
//if Modernizr determines "dd-mmm-yyyy" dates are not supported, load the following JavaScript resources
Modernizr.load([
{
test: Modernizr.valid_date_dd_mmm_yyyy,
nope: 'http://appliedinter.net/Workstream/common_files/js/dataTable_shim_dd-MMM-yyyy.js',
complete: function () {
$(document).ready(function () {
//now call the dataTable plugin against the target tables (in this case, any table with `class="dataTable"`)
$('table.datatable').dataTable();
});
}
}
]);
See the Modernizr approach at http://jsfiddle.net/jhfrench/tNkGC/