Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created September 19, 2016 11:21
Show Gist options
  • Save andreasvirkus/a0db7bb45800acf024fc69d56057821a to your computer and use it in GitHub Desktop.
Save andreasvirkus/a0db7bb45800acf024fc69d56057821a to your computer and use it in GitHub Desktop.
// Forked from https://github.com/tristen/tablesort/blob/gh-pages/src/sorts/tablesort.date.js
// Basic dates in dd/mm/yy, dd-mm-yy or dd.mm.yy format.
// Years can be 4 digits. Days and Months can be 1 or 2 digits.
(function(){
// Change $1/$2/$3 order to $2/$1/$3 to support dd/mm/yy format instead of mm/dd/yy
var dateFormat = '$2/$1/$3';
var parseDate = function(date) {
date = date.replace(/\-/g, '/');
date = date.replace(/(\d{1,2})[\/\-.](\d{1,2})[\/\-.](\d{2})/, dateFormat); // format before getTime
return new Date(date).getTime() || -1;
};
Tablesort.extend('date', function(item) {
return (
item.search(/(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\.?,?\s*/i) !== -1 ||
item.search(/\d{1,2}[\/\-.]\d{1,2}[\/\-.]\d{2,4}/) !== -1 ||
item.search(/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/i) !== -1
) && !isNaN(parseDate(item));
}, function(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return parseDate(b) - parseDate(a);
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment