tablesorter
tablesorter copied to clipboard
Dateparser fails if trailing text
if you have a column like this: 2023-01-01: Text
The shortDate parser is applied to this column because the regex tests for ^datefomat The format function "just" reformats the date in this column and keeps trailling text. Therefore this becomes "2023/01/01: Text" which is applied to this:
var date = new Date( dateString );
return date instanceof Date && isFinite( date ) ? date.getTime() : '';
This will always be false. As new Date("2023/01/01: Text") returns false.
this can be solved: a) don't apply this filter if we have trailling text (regex with $ at the end) b) remove trailling text if the format function is called. c) format all dates as yyyy-mm-dd and just apply the natural sorting algorithm - so trailling text is sorted as well
Solution c looks best to me.
Try using one of the parsers in parser-date-extract.js
this does not help, if you want this to be done automatically.
Added a simple custom parser:
$.tablesorter.addParser({
id: 'newShortDate', // 'mmddyyyy', 'ddmmyyyy' or 'yyyymmdd'
is: function(str){
str = (str || '').replace($.tablesorter.regex.spaces, ' ').replace($.tablesorter.regex.shortDateReplace, '/');
return $.tablesorter.regex.shortDateTest.test(str);
},
format: function(str, table, cell, cellIndex){
return str;
},
type: 'text'
});
But it should be fixed.