Adjust Date Format

Automatically adjusts date format, short day name, and timezone

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Adjust Date Format
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically adjusts date format, short day name, and timezone
// @author       canofpaint
// @match        https://8chan.se/*
// @match        https://8chan.moe/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to reformat dates and adjust time zones
    function reformatDates() {
        const userLang = navigator.language || 'en-US'; // Detect the user's language for formatting
        const dateElements = document.querySelectorAll('.labelCreated'); // Find all elements with class `.labelCreated`

        dateElements.forEach((element) => {
            const originalText = element.textContent.trim();
            // Match MM/DD/YYYY (Day) HH:mm:ss pattern
            const match = originalText.match(/^(\d{2})\/(\d{2})\/(\d{4}) \((\w{3})\) (\d{2}):(\d{2}):(\d{2})$/);

            if (match) {
                const [, month, day, year, dayAbbr, hours, minutes, seconds] = match;

                // Convert the original date and time (UTC) to a JavaScript Date object
                const originalDateUTC = new Date(Date.UTC(
                    parseInt(year, 10),
                    parseInt(month, 10) - 1,
                    parseInt(day, 10),
                    parseInt(hours, 10),
                    parseInt(minutes, 10),
                    parseInt(seconds, 10)
                ));

                // Adjust to the user's local time zone
                const localDate = new Date(originalDateUTC.getTime() - (originalDateUTC.getTimezoneOffset() * 60000));

                // Format the date using the user's language
                const shortDayName = new Intl.DateTimeFormat(userLang, { weekday: 'short' }).format(localDate);
                const formattedDay = localDate.getDate().toString().padStart(2, '0');
                const formattedMonth = (localDate.getMonth() + 1).toString().padStart(2, '0');
                const formattedYear = localDate.getFullYear();
                const formattedTime = localDate.toLocaleTimeString(userLang, { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false });

                // Replace the original text with the reformatted date in the specified order
                element.textContent = `${formattedDay}-${formattedMonth}-${formattedYear} (${shortDayName}) ${formattedTime}`;
            }
        });
    }

    // Observe the page for any updates to dynamically added content
    const observer = new MutationObserver(reformatDates);
    observer.observe(document.body, { childList: true, subtree: true });

    // Run the date reformatting function when the page loads
    reformatDates();
})();