Greasy Fork

Greasy Fork is available in English.

X (Twitter) 动态时间格式化

根据日期智能显示时间:今天(HH:mm)、今年(MM-DD HH:mm)、往年(YYYY-MM-DD HH:mm)

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         X (Twitter) 动态时间格式化
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  根据日期智能显示时间:今天(HH:mm)、今年(MM-DD HH:mm)、往年(YYYY-MM-DD HH:mm)
// @author       GeBron
// @match        https://x.com/*
// @match        https://twitter.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    function formatTime() {
        const timeElements = document.querySelectorAll('time');
        const now = new Date();

        timeElements.forEach(timeEl => {
            if (timeEl.dataset.absoluteTimeReady) return;

            const datetime = timeEl.getAttribute('datetime');
            if (datetime) {
                const date = new Date(datetime);
                
                const isToday = date.toDateString() === now.toDateString();
                const isThisYear = date.getFullYear() === now.getFullYear();

                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                const hours = String(date.getHours()).padStart(2, '0');
                const minutes = String(date.getMinutes()).padStart(2, '0');

                let displayTime;

                if (isToday) {
                    // 1. 如果是当天:14:30
                    displayTime = `${hours}:${minutes}`;
                } else if (isThisYear) {
                    // 2. 如果是当年:10-27 14:30
                    displayTime = `${month}-${day} ${hours}:${minutes}`;
                } else {
                    // 3. 如果是往年:2023-10-27 14:30
                    displayTime = `${year}-${month}-${day} ${hours}:${minutes}`;
                }

                const span = timeEl.querySelector('span');
                if (span) {
                    span.textContent = displayTime;
                } else {
                    timeEl.textContent = displayTime;
                }

                timeEl.dataset.absoluteTimeReady = "true";
            }
        });
    }

    const observer = new MutationObserver((mutations) => {
        for (let mutation of mutations) {
            if (mutation.addedNodes.length > 0) {
                formatTime();
                break;
            }
        }
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    formatTime();
})();