Categories
Uncategorized

custom date parser for jQuery tablesorter


I have a project that stores dates in “Feb 09, 2010” format and I’ve been loving the jQuery plugin – tablesorter – for the flexibility and convenience it offers. Unfortunately, the three parsers that ship as part of the tablesorter plugin (isoDate, usLongDate, and shortDate) never seem to work correctly when attempting to sort the dates in my tables. So, after banging on it for a while, I’ve come up w/ the following date parser. Feel free to alter as much as you’d like.


$(function() {
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";
$.tablesorter.addParser({
id: 'bishDate',
is: function(s) {
return false;
},
format: function(s) {
var hit = s.match(/([A-Za-z]{3})s(d{2}),s(d{4})/);
var m = hit[1];
m = monthNames[m];
var d = "0" + hit[2];
d = d.substr(d.length - 2);
var y = hit[3];
return "" + y + m + d;
},
type: 'numeric'
});

// then force your custom parser on the column you need
$("#tproc").tablesorter({
headers: {
5: {sorter: 'bishDate'}
}
});

By bishless

just some guy.