Greasy Fork is available in English.

ISO 8601 Dates

Display US dates in the ISO 8601 YYYY-MM-DD format

  1. // ==UserScript==
  2. // @name ISO 8601 Dates
  3. // @description Display US dates in the ISO 8601 YYYY-MM-DD format
  4. // @author chocolateboy
  5. // @copyright chocolateboy
  6. // @version 1.2.2
  7. // @namespace https://github.com/chocolateboy/userscripts
  8. // @license GPL: http://www.gnu.org/copyleft/gpl.html
  9. // @exclude *
  10. // @grant GM_registerMenuCommand
  11. // ==/UserScript==
  12.  
  13. var XPATH = '//body//text()';
  14. var DATE = new RegExp(
  15. '(?:\\b(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-(\\d{4}|\\d{2})\\b(?!-))'
  16. + '|'
  17. + '(?:\\b(0[1-9]|1[0-2])/(0[1-9]|[12][0-9]|3[01])/(\\d{4}|\\d{2})\\b(?!/))',
  18. 'g'
  19. );
  20.  
  21. // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
  22. var days = [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
  23.  
  24. function leap_year (year) {
  25. if ((year % 400) === 0) { // multiples of 400 are leap years
  26. return true;
  27. } else if ((year % 100) === 0) { // the remaining multiples of 100 are not leap years
  28. return false;
  29. } else if ((year % 4) === 0) { // the remaining multiples of 4 are leap years
  30. return true;
  31. } else { // the rest are common years
  32. return false;
  33. }
  34. }
  35.  
  36. // https://bugzilla.mozilla.org/show_bug.cgi?id=392378
  37. function replace (match, m1, d1, y1, m2, d2, y2, offset, string) {
  38. var year = y1 || y2; // depending on the above, non-matches are either empty or undefined, both of which are false
  39. var month = m1 || m2;
  40. var day = d1 || d2;
  41.  
  42. // manual negative look-behind: see: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript
  43. if (offset > 0) {
  44. var prefix = string[offset - 1];
  45.  
  46. if ((prefix === '-') || (prefix === '/')) {
  47. return match;
  48. }
  49. }
  50.  
  51. if (day > days[month - 1]) {
  52. return match;
  53. }
  54.  
  55. if (year.length === 2) {
  56. // Internet Founding Fathers, forgive us. From the epoch to 1999, we knew not what to do...
  57. year = (((year >= 70) && (year <= 99)) ? '19' : '20') + year;
  58. }
  59.  
  60. if ((month === '02') && (day === '29') && !leap_year(year)) {
  61. return match;
  62. }
  63.  
  64. return year + '-' + month + '-' + day;
  65. }
  66.  
  67. function fix_dates () {
  68. var nodes = document.evaluate(XPATH, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  69.  
  70. for (var i = 0; i < nodes.snapshotLength; ++i) {
  71. var text = nodes.snapshotItem(i);
  72. text.data = text.data.replace(DATE, replace);
  73. }
  74. }
  75.  
  76. // add a menu command for that pesky, hard-to-reach, lazily-loaded content
  77. GM_registerMenuCommand('ISO 8601 Dates', fix_dates);
  78.  
  79. // run this script as late as possible to handle dynamically loaded content e.g. cracked.com
  80. window.addEventListener('load', fix_dates, false);