jQuery tablesorter自定义日期格式 [英] jQuery tablesorter custom date format
问题描述
我目前使用 Tablesorter (一个jQuery插件)对表格进行排序。我试图用yyyy MMM dd格式对日期进行排序,但似乎我无法做到这一点。我需要说我的日期输入是法语,如下所示:
I currently use Tablesorter (a jQuery plugin) to sort a table. I'm trying to sort date in the yyyy MMM dd format but it appears that im not able to do this. I need to say that my date inputs are in french like so :
- 2014 janv。 05
- 2013févr。 03
- 2011年火星02
我尝试了很多东西,但它没有以正确的方式排序。我不知道这是因为我的日期输入还是用法语或其他什么,但是我将要放弃它。
I tried a lot of things but it just don't sort the right way. I don't know if it's because my date inputs or in french or whatever but i'm about to give it up on this.
以下是我使用的代码
$.tablesorter.addParser({
id: "date",
is: function (s) {
return false;
},
format: function (s, table) {
var date = s.split(' ');
var month = translateMonth(date[1]);
var d = new Date(date[0], month, date[2]);
console.log(d.toString());
return d.getTime();
},
type: "numeric"
});
和
$("#table").tablesorter({
headers: {
2: {
sorter: 'date'
}
}
});
和
function translateMonth(month) {
switch (month) {
case "janv.": return 0;
case "févr.": return 1;
case "mars": return 2;
case "avril": return 3;
case "mai": return 4;
case "juin": return 5;
case "juil": return 6;
case "août.": return 7;
case "sept.": return 8;
case "oct.": return 9;
case "nov.": return 10;
case "déc.": return 11;
default: return -1;
}
}
我年和我的天 正确排序但这里的问题是我的月份
my years and my days are sorted correctly but the problem here are my months
我会很感激任何帮助
谢谢 - S
推荐答案
你是对的,它不知道这些月缩写是什么。有一些日期库可以支持这类事情,例如 moment.js ,如果你是一个很好的工具'能够使用jquery。如果没有,那么只需将缩写转换为适当的月份数字。
You're right, it doesn't know what those month abbreviations are. There are date libraries out there that can support that kind of thing, such as moment.js which is a great tool if you're able to use jquery. If not, then just convert your abbreviations to the appropriate month digit.
function translateMonth(month)
{
switch (month)
{
case 'janv.': return 0;
case 'févr.': return 1;
case 'févr.': return 2;
case 'mars': return 3;
case 'avril': return 4;
case 'mai': return 5;
case 'juin': return 6;
case 'juil.': return 7;
case 'août': return 8;
case 'oct.': return 9;
case 'nov.': return 10;
case 'déc.': return 11;
default: return -1;
}
}
$.tablesorter.addParser({
id: "customParser",
is: function (s) {
return false;
},
format: function (s) {
var date = s.split(' '),
month = translateMonth(date[1]);
if(month >= 0)
return new Date(date[0], month ,date[2]).getTime();
else
return new Date().getTime();
},
type: 'numeric'
});
这篇关于jQuery tablesorter自定义日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!