PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/webster5361/UserFrosting
JavaScript | 80 lines | 58 code | 8 blank | 14 comment | 6 complexity | a5877ff207c4912628d56e72a8ac0933 MD5 | raw file
 1/*! Parser: two digit year - updated 10/26/2014 (v2.18.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	ts.dates = $.extend({}, ts.dates, {
17		regxxxxyy: /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/,
18		regyyxxxx: /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/
19	});
20
21	ts.formatDate = function(s, regex, format, table){
22		if (s) {
23			var y, rng,
24				n = s
25					// replace separators
26					.replace(/\s+/g, ' ').replace(/[-.,]/g, '/')
27					// reformat xx/xx/xx to mm/dd/19yy;
28					.replace(regex, format),
29				d = new Date(n);
30			if ( d instanceof Date && isFinite(d) ) {
31				y = d.getFullYear();
32				rng = table && table.config.dateRange || range;
33				// if date > 50 years old (set range), add 100 years
34				// this will work when people start using '50' and mean '2050'
35				while (now - y > rng) {
36					y += 100;
37				}
38				return d.setFullYear(y);
39			}
40		}
41		return s;
42	};
43
44	$.tablesorter.addParser({
45		id: 'ddmmyy',
46		is: function() {
47			return false;
48		},
49		format: function(s, table) {
50			// reformat dd/mm/yy to mm/dd/19yy;
51			return ts.formatDate(s, ts.dates.regxxxxyy, '$2/$1/19$3', table);
52		},
53		type: 'numeric'
54	});
55
56	$.tablesorter.addParser({
57		id: 'mmddyy',
58		is: function() {
59			return false;
60		},
61		format: function(s, table) {
62			// reformat mm/dd/yy to mm/dd/19yy
63			return ts.formatDate(s, ts.dates.regxxxxyy, '$1/$2/19$3', table);
64		},
65		type: 'numeric'
66	});
67
68	$.tablesorter.addParser({
69		id: 'yymmdd',
70		is: function() {
71			return false;
72		},
73		format: function(s, table) {
74			// reformat yy/mm/dd to mm/dd/19yy
75			return ts.formatDate(s, ts.dates.regyyxxxx, '$2/$3/19$1', table);
76		},
77		type: 'numeric'
78	});
79
80})(jQuery);