Add dateRange option to two-digit year parser

This commit is contained in:
Mottie 2013-11-13 20:31:26 -06:00
parent cfcf0efe0c
commit ae72017a7b
2 changed files with 13 additions and 9 deletions

View File

@ -33,7 +33,9 @@
// call the tablesorter plugin
$("table").tablesorter({
theme : 'blue',
widgets : ["zebra"]
widgets : ["zebra"],
// date range used by the two-digit year parser (added v2.14.0)
dateRange : 30
});
});</script>
@ -62,6 +64,7 @@
<li>So if the current year is <code>2020</code>, and the listed two digit year is <code>80</code> (<code>2080 - 2020 > 50</code>), it becomes <code>1980</code>.</li>
<li>If the listed two digit year is <code>50</code> (<code>2050 - 2020 < 50</code>), then it becomes <code>2050</code>. I hope that makes it clearer.</li>
<li>Try out the two digit year calculator below the table.</li>
<li>In <span class="version">v2.14.0</span>, the range can be set using the <code>dateRange</code> option (see the initialization code below).</li>
</ul>
</li>
<li>The "Time" column is using the built-in time parser which has been always been included with tablesorter .</li>

View File

@ -17,7 +17,7 @@
regyyxxxx: /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/
});
ts.formatDate = function(s, regex, format){
ts.formatDate = function(s, regex, format, table){
s = s
// replace separators
.replace(/\s+/g," ").replace(/[-.,]/g, "/")
@ -25,10 +25,11 @@
.replace(regex, format);
var d = new Date(s),
y = d.getFullYear(),
rng = table && table.config.dateRange || range,
now = new Date().getFullYear();
// if date > 50 years old (set range), add 100 years
// this will work when people start using "50" and mean "2050"
while (now - y > range) {
while (now - y > rng) {
y += 100;
}
return d.setFullYear(y);
@ -39,9 +40,9 @@
is: function() {
return false;
},
format: function(s) {
format: function(s, table) {
// reformat dd/mm/yy to mm/dd/19yy;
return ts.formatDate(s, ts.dates.regxxxxyy, "$2/$1/19$3");
return ts.formatDate(s, ts.dates.regxxxxyy, "$2/$1/19$3", table);
},
type: "numeric"
});
@ -51,9 +52,9 @@
is: function() {
return false;
},
format: function(s) {
format: function(s, table) {
// reformat mm/dd/yy to mm/dd/19yy
return ts.formatDate(s, ts.dates.regxxxxyy, "$1/$2/19$3");
return ts.formatDate(s, ts.dates.regxxxxyy, "$1/$2/19$3", table);
},
type: "numeric"
});
@ -63,9 +64,9 @@
is: function() {
return false;
},
format: function(s) {
format: function(s, table) {
// reformat yy/mm/dd to mm/dd/19yy
return ts.formatDate(s, ts.dates.regyyxxxx, "$2/$3/19$1");
return ts.formatDate(s, ts.dates.regyyxxxx, "$2/$3/19$1", table);
},
type: "numeric"
});