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)>
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"
});
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
.
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.