jQuery tablesorter排序日期dd mmm yyyy [英] jquery tablesorter sort date dd mmm yyyy
问题描述
我对tablesorter还是很陌生,但是我有一个包含几列的表,第三列是日期,以dd mmm yyyy, hh:mm:ss (am/pm)
I am pretty new to tablesorter but I have a table with several columns, and the 3rd column is dates, coming back from the DB in the form of dd mmm yyyy, hh:mm:ss (am/pm)
例如
29 Jul 2013, 1:12:23 PM
2 Aug 2013, 3:59:59 PM
17 Jul 2013, 09:30:00 AM
然后我对桌子进行排序
$(document).ready(function()
{
$("#myTable").tablesorter({sortInitialOrder: "desc"});
}
);
,但它以字母顺序"出现,表示29之前2之前17之前,而不考虑日期方面.
but it comes out in "alphabetical order", meaning 29 before 2 before 17, not taking into account the date aspects.
似乎有一个可以传递的dateFormat选项,但我无法使用它.我知道Java具有具有不同含义的特殊键,像这样一个>.在tablesorter中有什么可比的吗?如何确保一日期栏的排序正确?
It looks like there is a dateFormat option I can pass in but I can't get it to work. I know Java has special keys with different meanings like this. Is there anything comparable in tablesorter? How do I go about making sure the one date column is sorted properly?
我可以保证格式始终与上面指定的相同,并且我不想更改显示日期的外观,而只是更改排序功能.
I can guarantee that the format will always be as I specified above, and I don't want to change the appearance of the displayed date, just the sorting functionality.
看起来像dateFormat:'usLongDate'接近我需要的但不能正常工作
Looks like dateFormat: 'usLongDate' is close to what I need but not working
更新:我认为我的问题是我在对象中拥有的信息不仅限于日期,但是我想仅按日期排序.这是我的工作 jsFiddle .
UPDATE: I think my problem is that I have more info in the object than just the date, however I want to sort by just the date. Here is my working jsFiddle.
推荐答案
尝试使用此解析器(由于它不验证日期,即它接受时间为99:99:99
,因此并不完美;但是日期解析器将接受返回无效的日期并默认返回纯文本(演示):
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"
});
更新:要详细说明正则表达式并回答您的评论...基本上,正则表达式匹配一个模式:\d{1,2}
匹配任何1或2位数字,\d{4}
匹配任何4位数字,\w{3}
匹配任何单词" 长度为3个字母的\s+
匹配任意数量的空格或制表符,而[AP]M
匹配AM
或PM
.
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
.
现在,括号()
保存特定的匹配项-请注意逗号在括号之外.并且请注意,数组中的第一个值(如果匹配)是整个字符串.因此,第一部分date[1]
包含日期部分(例如29 Jul 2013
),第二部分date[2]
包含时间(例如1:12:23 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.
这篇关于jQuery tablesorter排序日期dd mmm yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!