jquery.tablesorter addParser for "26 Jul" date format
I want to sort dates which look like this:开发者_JS百科 26 Jul
This is my attempt at adding a parser:
var lMonthNames = {};
lMonthNames["Jan"] = "01";
lMonthNames["Feb"] = "02";
lMonthNames["Mar"] = "03";
lMonthNames["Apr"] = "04";
lMonthNames["May"] = "05";
lMonthNames["Jun"] = "06";
lMonthNames["Jul"] = "07";
lMonthNames["Aug"] = "08";
lMonthNames["Sep"] = "09";
lMonthNames["Oct"] = "10";
lMonthNames["Nov"] = "11";
lMonthNames["Dec"] = "12";
ts.addParser({
id: 'monthDay',
is: function(s) {
return false;
},
format: function(s) {
if (s.length > 0) {
var date = s.match(/^(\d{1,2})[ ](\w{3})[ ](\d{4})$/);
var m = lMonthNames[date[2]];
var d = String(date[1]);
if (d.length == 1) {d = "0" + d;}
return '' + m + d;
} else {
return '';
}
},
type: 'numeric'
});
But the firebug console tells me date is null for var m = lMonthNames[date[2]]; Can you pick why it doesn't have a value?
Because the regular expression you have doesn't match the data you are giving it.
Your's matches things like: 26 Jul 2010
and not things like 26 Jul
.
Either change the data in the table, or change the regular expression to be:
var date = s.match(/^(\d{1,2})[ ](\w{3})$/);
精彩评论