1

我正在使用以下脚本对表进行排序和过滤:

http://javascripttoolbox.com/lib/table/
源代码: http:
//javascripttoolbox.com/libsource.php/table/source/table.js

我的日期格式为:dd-MM-yyyy。

该脚本具有三个用于对日期进行排序的内置 RegEx 函数:

sort.date.formats = [
// YY[YY]-MM-DD
{
    re: /(\d{2,4})-(\d{1,2})-(\d{1,2})/,
    f: function (x) {
        return (new Date(sort.date.fixYear(x[1]), +x[2], +x[3])).getTime();
    }
}
// MM/DD/YY[YY] or MM-DD-YY[YY]
,
{
    re: /(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})/,
    f: function (x) {
        return (new Date(sort.date.fixYear(x[3]), +x[1], +x[2])).getTime();
    }
}
// Any catch-all format that new Date() can handle. This is not reliable except for long formats, for example: 31 Jan 2000 01:23:45 GMT
,
{
    re: /(.*\d{4}.*\d+:\d+\d+.*)/,
    f: function (x) {
        var d = new Date(x[1]);
        if (d) {
            return d.getTime();
        }
    }
}];

所以问题是,dd-MM-yyyy 格式的日期的正则表达式是什么样的?

我在这里创建了一个 jsFiddle:

http://jsfiddle.net/LgQsu/

如果您的解决方案适用于截止日期列,请告诉我!

4

2 回答 2

2

你的小提琴有一个额外的引用table.js,它正在执行而不是你的javascript代码。此外,要触发代码,需要将其插入到 head-tag 中(“Framework & Extensions”下的设置)。

您的截止日期列被指定为“默认”排序,即字母数字。

<th class="table-sortable:date ..." ...>

修复后,错误的日期格式匹配。日期匹配为“YY-MM-DD”(带有 2 位数年份),而不是“DD-MM-YYYY”,即使日期以 4 位数年份结尾。那是因为您的正则表达式没有锚定^and $

sort.date.formats = [
    // YY[YY]-MM-DD
    {
        re: /^\s*(\d{2,4})-(\d{1,2})-(\d{1,2})\s*$/,
        f: function (x) {
            return (new Date(sort.date.fixYear(x[1]),+x[2],+x[3])).getTime();
        }
    },
    // DD/MM/YY[YY] or DD-MM-YY[YY]
    {
        re: /^\s*(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})\s*$/,
        f: function (x) {
            return (new Date(sort.date.fixYear(x[3]),+x[2],+x[1])).getTime();
        }
    },
    // Any catch-all format that new Date() can handle. This is not reliable except for long formats, for example: 31 Jan 2000 01:23:45 GMT
    {
        re: /(.*\d{4}.*\d+:\d+\d+.*)/,
        f: function (x) {
            var d=new Date(x[1]);
            if (d) {
                return d.getTime();
            }
        }
    }
];

这是一个更新的小提琴,带有工作日期排序:

http://jsfiddle.net/fa2Qm/

于 2013-06-26T16:54:47.917 回答
0

正则表达式与第二个相同,即

re: /(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})/,

如果您考虑一下,dd-mm-yyyy 看起来与 mm-dd-yyyy 相同

但是,创建 Date 对象的参数顺序会发生变化,请注意 x 数组中索引的变化:

{
    re: /(\d{1,2})[\/-](\d{1,2})[\/-](\d{2,4})/,
    f: function (x) {
        return (new Date(sort.date.fixYear(x[3]), +x[2], +x[1])).getTime();
    }
}
于 2013-06-26T16:01:51.483 回答