javascript - 如何使用 tablesorter 按 javascript Date 对象进行排序?

标签 javascript sorting date tablesorter

我正在尝试对其中一列中包含 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"
    });

最佳答案

使用UTC date format :20111117,您可以按字母顺序排序。

关于javascript - 如何使用 tablesorter 按 javascript Date 对象进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8174622/

相关文章:

Javascript-将数组中的值匹配到变量的子字符串

带有 DataTable 的 Javascript JSON 数据

mysql - excel vba - 根据包含日期和时间的相邻单元格添加带有日期的表格列

javascript - 在javascript中等待文件下载时的进度/忙碌指示器?

javascript - 输入 keydown 事件触发按钮单击另一个元素

c# - 在没有 Linq 的情况下使用字符串对 List<T> 进行排序

javascript - 如何使用下划线根据自定义排序顺序对对象数组进行排序

java - 从整数集合中获取最大数的最有效方法

java - Java grails:无法将类 'Mon Feb 12 00:00:00 PKT 2018'的对象 'java.util.Date'强制转换为类 'org.joda.time.LocalDate'

postgresql - 在 postgres 中标准化日期 - 迄今为止的字符