简体   繁体   中英

Regular expression for sorting dates in european format (dd-MM-yyyy)

I am using the following script for sorting and filteriing tables:

http://javascripttoolbox.com/lib/table/
source code:
http://javascripttoolbox.com/libsource.php/table/source/table.js

My dates are in the format: dd-MM-yyyy.

The script has three built in RegEx functions for sorting dates:

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();
        }
    }
}];

So the question is, how does a regular expression for dates in the format dd-MM-yyyy look like?

I have created a jsFiddle here:

http://jsfiddle.net/LgQsu/

Please let me know if your solution works on the Deadline column!

Your fiddle had an additional reference to table.js , which was executing instead of your javascript code. Also, for the code to trigger, it needs to be inserted into the head-tag (a setting under "Framework & Extensions").

Your deadline-column was specified to sort as "default", which is alphanumeric.

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

After fixing that, the wrong date-format was matching. The dates was matching as "YY-MM-DD" (with 2-digit year), instead of "DD-MM-YYYY", even though the dates was ending with a 4-digit year. That is because your Regexes wasn't anchored with ^ 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();
            }
        }
    }
];

Here is an updated fiddle, with working date-sorting:

http://jsfiddle.net/fa2Qm/

The regex is the same as the second one ie

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

If you think about it dd-mm-yyyy looks the same as mm-dd-yyyy

The parameter order to create the Date object will change though, note the change of indexes into the x array:

{
    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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM