Debian Codegraph @

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

raw symbols SCIP
1 /*! Parser: two digit year - updated 11/26/2016 (v2.28.0) */
2 /* Demo: http://mottie.github.io/tablesorter/docs/example-parsers-dates.html */
3 /*jshint jquery:true */
4 ;(function($) {
5 'use strict';
6
7 // Make the date be within +/- range of the 2 digit year
8 // so if the current year is 2020, and the 2 digit year is 80 (2080 - 2020 > 50), it becomes 1980
9 // if the 2 digit year is 50 (2050 - 2020 < 50), then it becomes 2050.
10 var range = 50,
11
12 // no need to change any of the code below
13 ts = $.tablesorter,
14 now = new Date().getFullYear();
15
16 // add dateRange to defaults for validator; value must be falsy
17 ts.defaults.dataRange = '';
18
19 if ( !ts.dates ) { ts.dates = {}; }
20 ts.dates.regxxxxyy = /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/;
21 ts.dates.regyyxxxx = /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/;
22
23 ts.formatDate = function(s, regex, format, table) {
24 if (s) {
25 var y, rng,
26 n = s
27 // replace separators
28 .replace(/\s+/g, ' ').replace(/[-.,]/g, '/')
29 // reformat xx/xx/xx to mm/dd/19yy;
30 .replace(regex, format),
31 d = new Date(n);
32 if ( d instanceof Date && isFinite(d) ) {
33 y = d.getFullYear();
34 rng = table && table.config.dateRange || range;
35 // if date > 50 years old (set range), add 100 years
36 // this will work when people start using '50' and mean '2050'
37 while (now - y > rng) {
38 y += 100;
39 }
40 return d.setFullYear(y);
41 }
42 }
43 return s;
44 };
45
46 $.tablesorter.addParser({
47 id: 'ddmmyy',
48 is: function() {
49 return false;
50 },
51 format: function(s, table) {
52 // reformat dd/mm/yy to mm/dd/19yy;
53 return ts.formatDate(s, ts.dates.regxxxxyy, '$2/$1/19$3', table);
54 },
55 type: 'numeric'
56 });
57
58 $.tablesorter.addParser({
59 id: 'mmddyy',
60 is: function() {
61 return false;
62 },
63 format: function(s, table) {
64 // reformat mm/dd/yy to mm/dd/19yy
65 return ts.formatDate(s, ts.dates.regxxxxyy, '$1/$2/19$3', table);
66 },
67 type: 'numeric'
68 });
69
70 $.tablesorter.addParser({
71 id: 'yymmdd',
72 is: function() {
73 return false;
74 },
75 format: function(s, table) {
76 // reformat yy/mm/dd to mm/dd/19yy
77 return ts.formatDate(s, ts.dates.regyyxxxx, '$2/$3/19$1', table);
78 },
79 type: 'numeric'
80 });
81
82 })(jQuery);