jquery tablesorter sort date dd mmm yyyy

喜欢而已 提交于 2019-12-02 12:03:14

Try this parser (it's not perfect since it doesn't validate the date, i.e. it will accept a time of 99:99:99; but then the date parser will return an invalid date and default back to plain text (demo):

$.tablesorter.addParser({
    id: "date",
    is: function (s) {
        return false;
    },
    format: function (s, table) {
        var date = (s + '').match(/(\d{1,2}\s+\w{3}\s+\d{4}),(\s+\d{1,2}:\d{1,2}:\d{1,2}\s+[AP]M)/);
        return date ? new Date(date[1] + date[2]).getTime() || s : s;
    },
    type: "numeric"
});

Update: To elaborate on the regex and answer your comment... basically the regex is matching a pattern: \d{1,2} matches any 1 or 2 digits, \d{4} matches any 4 digits, \w{3} matches any "word" 3 letters in length, \s+ matches any number of spaces or tabs and [AP]M matches AM or PM.

Now the parenthesis () save specific matches - notice that the comma is outside of the parentheses. And note that the first value in the array is the entire string, if it matches. So the first part date[1] contains the date portion (e.g. 29 Jul 2013) and the second part date[2] contains the time (e.g. 1:12:23 PM).

Check out this basic tutorial on regular expressions, then try messing around with this regex tester.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!