我正在尝试使用其中一列中的javascript Date对象对表进行排序。日期的形式是mm / dd / yyyy hr:min a.m.当我尝试按日期排序时,我点击标题,没有任何反应。我可以按其他列排序就好了。添加了console.log语句用于调试目的 - 您可以忽略它们。
$.tablesorter.addParser({
id: "customDate",
is: function(s) {
//return false;
//use the above line if you don't want table sorter to auto detected this parser
//else use the below line.
//attention: doesn't check for invalid stuff
//2009-77-77 77:77:77.0 would also be matched
//if that doesn't suit you alter the regex to be more restrictive
var passes = /\d{1,2}\/\d{1,2}\/\d{1,4}\s\d{1,2}:\d{1,2}\s[ap]\.m\./.test(s);
return passes;
},
format: function(s) {
s = s.replace(/\-/g," ");
s = s.replace(/:/g," ");
s = s.replace(/\./g," ");
s = s.replace(/\//g," ");
s = s.replace(/a\sm/, 1);
s = s.replace(/p\sm/, 2);
s = s.split(" ");
console.log('am or pm: ' + s[5]);
console.log('hour == ' + s[3]);
if (s[3] == '12' && s[5] == 1){
s[3] = 0; //convert 12am to 0 hours.
console.log('new hour -- 12am: ' + s[3]);
}
else if (s[5] == 2 && s[3] != '12'){
s[3] = parseInt(s[3]) + 12; //convert p.m. hours to military time format if it isn't noon.
console.log('new hour -- pm: ' + s[3]);
}
console.log('minutes: ' + parseInt(s[4]));
console.log();
return $.tablesorter.formatFloat(new Date(s[2], s[0], s[1], s[3], s[4],0,0,0));
},
type: "numeric"
});
答案 0 :(得分:0)
使用UTC date format:20111117,您可以按字母顺序对其进行排序。
答案 1 :(得分:0)
好吧,我解决了自己的问题。如果我在我的日期对象上调用getTime(),它会返回日期的数值,并且我可以对其进行排序。
现在这行代码如下:
return $.tablesorter.formatFloat(new Date(s[2], s[0], s[1], s[3], s[4],0,0,0).getTime());