正确的做法是为这种自定义格式添加您自己的解析器。
检查这个以了解它是如何工作的。
jQuery Tablesorter:添加您自己的解析器 https://mottie.github.io/tablesorter/docs/example-parsers.html
然后查看 tablesorter 源代码(搜索 uslongdate、shortdate,然后观察 tablesorter 内部如何完成日期格式的解析器。现在为您的特定日期格式构建一个类似的解析器。
jquery.tablesorter.js https://mottie.github.io/tablesorter/docs/index.html
这应该符合你的喜好
ts.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
return /\d{1,4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}\.\d+/.test(s);
},
format: function(s) {
s = s.replace(/\-/g," ");
s = s.replace(/:/g," ");
s = s.replace(/\./g," ");
s = s.split(" ");
return $.tablesorter.formatFloat(new Date(s[0], s[1]-1, s[2], s[3], s[4], s[5]).getTime()+parseInt(s[6]));
},
type: "numeric"
});
现在只需将其应用到具有此格式的列(例如,假设您的自定义日期所在的列位于第 7 列中。(6 表示第 7 列,因为列的数组是从零开始的)
$(function() {
$("table").tablesorter({
headers: {
6: { sorter:'customDate' }
}
});
});