tablesorter/js/parsers/parser-date-two-digit-year.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-11-27 01:40:00 +00:00
/*! Parser: two digit year - updated 11/26/2016 (v2.28.0) */
/* Demo: http://mottie.github.io/tablesorter/docs/example-parsers-dates.html */
2013-03-26 21:05:21 +00:00
/*jshint jquery:true */
2018-03-17 20:30:25 +00:00
;(function($) {
'use strict';
2013-03-26 21:05:21 +00:00
// Make the date be within +/- range of the 2 digit year
// so if the current year is 2020, and the 2 digit year is 80 (2080 - 2020 > 50), it becomes 1980
// if the 2 digit year is 50 (2050 - 2020 < 50), then it becomes 2050.
var range = 50,
// no need to change any of the code below
ts = $.tablesorter,
now = new Date().getFullYear();
2013-09-29 19:37:03 +00:00
// add dateRange to defaults for validator; value must be falsy
ts.defaults.dataRange = '';
if ( !ts.dates ) { ts.dates = {}; }
ts.dates.regxxxxyy = /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/;
ts.dates.regyyxxxx = /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/;
2013-03-26 21:05:21 +00:00
2018-03-17 20:30:25 +00:00
ts.formatDate = function(s, regex, format, table) {
if (s) {
var y, rng,
n = s
// replace separators
.replace(/\s+/g, ' ').replace(/[-.,]/g, '/')
// reformat xx/xx/xx to mm/dd/19yy;
.replace(regex, format),
d = new Date(n);
if ( d instanceof Date && isFinite(d) ) {
y = d.getFullYear();
rng = table && table.config.dateRange || range;
// if date > 50 years old (set range), add 100 years
// this will work when people start using '50' and mean '2050'
while (now - y > rng) {
y += 100;
}
return d.setFullYear(y);
}
2013-03-26 21:05:21 +00:00
}
return s;
2013-03-26 21:05:21 +00:00
};
$.tablesorter.addParser({
id: 'ddmmyy',
2013-03-26 21:05:21 +00:00
is: function() {
return false;
},
format: function(s, table) {
2013-03-26 21:05:21 +00:00
// reformat dd/mm/yy to mm/dd/19yy;
return ts.formatDate(s, ts.dates.regxxxxyy, '$2/$1/19$3', table);
2013-03-26 21:05:21 +00:00
},
type: 'numeric'
2013-03-26 21:05:21 +00:00
});
$.tablesorter.addParser({
id: 'mmddyy',
2013-03-26 21:05:21 +00:00
is: function() {
return false;
},
format: function(s, table) {
2013-03-26 21:05:21 +00:00
// reformat mm/dd/yy to mm/dd/19yy
return ts.formatDate(s, ts.dates.regxxxxyy, '$1/$2/19$3', table);
2013-03-26 21:05:21 +00:00
},
type: 'numeric'
2013-03-26 21:05:21 +00:00
});
$.tablesorter.addParser({
id: 'yymmdd',
2013-03-26 21:05:21 +00:00
is: function() {
return false;
},
format: function(s, table) {
2013-03-26 21:05:21 +00:00
// reformat yy/mm/dd to mm/dd/19yy
return ts.formatDate(s, ts.dates.regyyxxxx, '$2/$3/19$1', table);
2013-03-26 21:05:21 +00:00
},
type: 'numeric'
2013-03-26 21:05:21 +00:00
});
})(jQuery);