im trying to write a parser for the jquery tablesorter plugin and i really need a hand on this… i got this date format: dd/mm/yyyy hh:mm:ss a.m.|p.m. and i cant make a proper regex to use javascript .match function… , here’s the code i got:
$.tablesorter.addParser({
id: 'DateParser',
is: function(s) {
return false;
},
format: function(s) {
var date = s.match(/^(\d{1,2})-(\d{1,2})-(\d{4}) :(\d{1,2}):(\d{1,2})$/)
var d = date[1];
var m = date[2];
var y = date[3];
var H = date[4];
var M = date[5];
var S = date[6];
var MS = 0;
return new Date(y, m, d, H, M, S, MS).getTime();
},
type: 'numeric'
});
$("#tabletosort").tablesorter({
headers: {
0: {
sorter: 'DateParser'
}
}
});
the result of the match() call is always null… thanks a lot in advance!
Your current regex is matching dates in the form
dd-mm-yyyy :mm:ss– you’ve missed out the hours, the am/pm, and used-instead of/, so that’s why it’s not ever matching dates formatted withdd/mm/yyyy hh:mm:ss a.m.|p.m.. Correcting those issues gives you something like the following:…and then you’d test the last sub-expression to adjust the hours for am or pm.
(Note: I don’t think you want the minutes and seconds to allow single digits, so I’ve changed that part to require two digits.)