/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. // Make the date be within +/- range of the 2 digit year
  7. // so if the current year is 2020, and the 2 digit year is 80 (2080 - 2020 > 50), it becomes 1980
  8. // if the 2 digit year is 50 (2050 - 2020 < 50), then it becomes 2050.
  9. var range = 50,
  10. // no need to change any of the code below
  11. ts = $.tablesorter,
  12. now = new Date().getFullYear();
  13. ts.dates = $.extend({}, ts.dates, {
  14. regxxxxyy: /(\d{1,2})[\/\s](\d{1,2})[\/\s](\d{2})/,
  15. regyyxxxx: /(\d{2})[\/\s](\d{1,2})[\/\s](\d{1,2})/
  16. });
  17. ts.formatDate = function(s, regex, format, table){
  18. if (s) {
  19. var y, rng,
  20. n = s
  21. // replace separators
  22. .replace(/\s+/g, ' ').replace(/[-.,]/g, '/')
  23. // reformat xx/xx/xx to mm/dd/19yy;
  24. .replace(regex, format),
  25. d = new Date(n);
  26. if ( d instanceof Date && isFinite(d) ) {
  27. y = d.getFullYear();
  28. rng = table && table.config.dateRange || range;
  29. // if date > 50 years old (set range), add 100 years
  30. // this will work when people start using '50' and mean '2050'
  31. while (now - y > rng) {
  32. y += 100;
  33. }
  34. return d.setFullYear(y);
  35. }
  36. }
  37. return s;
  38. };
  39. $.tablesorter.addParser({
  40. id: 'ddmmyy',
  41. is: function() {
  42. return false;
  43. },
  44. format: function(s, table) {
  45. // reformat dd/mm/yy to mm/dd/19yy;
  46. return ts.formatDate(s, ts.dates.regxxxxyy, '$2/$1/19$3', table);
  47. },
  48. type: 'numeric'
  49. });
  50. $.tablesorter.addParser({
  51. id: 'mmddyy',
  52. is: function() {
  53. return false;
  54. },
  55. format: function(s, table) {
  56. // reformat mm/dd/yy to mm/dd/19yy
  57. return ts.formatDate(s, ts.dates.regxxxxyy, '$1/$2/19$3', table);
  58. },
  59. type: 'numeric'
  60. });
  61. $.tablesorter.addParser({
  62. id: 'yymmdd',
  63. is: function() {
  64. return false;
  65. },
  66. format: function(s, table) {
  67. // reformat yy/mm/dd to mm/dd/19yy
  68. return ts.formatDate(s, ts.dates.regyyxxxx, '$2/$3/19$1', table);
  69. },
  70. type: 'numeric'
  71. });
  72. })(jQuery);