Greasy Fork is available in English.

Adjust Date Format

Automatically adjusts date format, short day name, and timezone

  1. // ==UserScript==
  2. // @name Adjust Date Format
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Automatically adjusts date format, short day name, and timezone
  6. // @author canofpaint
  7. // @match https://8chan.se/*
  8. // @match https://8chan.moe/*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // Function to reformat dates and adjust time zones
  17. function reformatDates() {
  18. const userLang = navigator.language || 'en-US'; // Detect the user's language for formatting
  19. const dateElements = document.querySelectorAll('.labelCreated'); // Find all elements with class `.labelCreated`
  20.  
  21. dateElements.forEach((element) => {
  22. const originalText = element.textContent.trim();
  23. // Match MM/DD/YYYY (Day) HH:mm:ss pattern
  24. const match = originalText.match(/^(\d{2})\/(\d{2})\/(\d{4}) \((\w{3})\) (\d{2}):(\d{2}):(\d{2})$/);
  25.  
  26. if (match) {
  27. const [, month, day, year, dayAbbr, hours, minutes, seconds] = match;
  28.  
  29. // Convert the original date and time (UTC) to a JavaScript Date object
  30. const originalDateUTC = new Date(Date.UTC(
  31. parseInt(year, 10),
  32. parseInt(month, 10) - 1,
  33. parseInt(day, 10),
  34. parseInt(hours, 10),
  35. parseInt(minutes, 10),
  36. parseInt(seconds, 10)
  37. ));
  38.  
  39. // Adjust to the user's local time zone
  40. const localDate = new Date(originalDateUTC.getTime() - (originalDateUTC.getTimezoneOffset() * 60000));
  41.  
  42. // Format the date using the user's language
  43. const shortDayName = new Intl.DateTimeFormat(userLang, { weekday: 'short' }).format(localDate);
  44. const formattedDay = localDate.getDate().toString().padStart(2, '0');
  45. const formattedMonth = (localDate.getMonth() + 1).toString().padStart(2, '0');
  46. const formattedYear = localDate.getFullYear();
  47. const formattedTime = localDate.toLocaleTimeString(userLang, { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });
  48.  
  49. // Replace the original text with the reformatted date in the specified order
  50. element.textContent = `${formattedDay}-${formattedMonth}-${formattedYear} (${shortDayName}) ${formattedTime}`;
  51. }
  52. });
  53. }
  54.  
  55. // Observe the page for any updates to dynamically added content
  56. const observer = new MutationObserver(reformatDates);
  57. observer.observe(document.body, { childList: true, subtree: true });
  58.  
  59. // Run the date reformatting function when the page loads
  60. reformatDates();
  61. })();