MT Override Date to Beijing Time

Override JavaScript's Date object to always use Beijing time (UTC+8)

目前为 2024-04-25 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/493489/1366373/MT%20Override%20Date%20to%20Beijing%20Time.js

// ==UserScript==
// @name         MT Override Date to Beijing Time
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Override JavaScript's Date object to always use Beijing time (UTC+8)
// @author       tttsc, GPT4
// @match        https://*.m-team.cc/*
// @match        https://*.m-team.io/*
// @match        https://test2.m-team.cc/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const originalDate = Date;

    // Function to adjust the local time to Beijing time
    function toBeijingTime(original) {
        const localTime = new originalDate(original);
        const localTimeMs = localTime.getTime();
        const localOffset = localTime.getTimezoneOffset() * 60000; // Offset in milliseconds
        const beijingOffset = 8 * 3600 * 1000; // Beijing is UTC+8
        return new originalDate(localTimeMs + localOffset + beijingOffset);
    }

    // Override the Date object
    Date = function () {
        if (arguments.length === 0) {
            return toBeijingTime(originalDate.now());
        } else if (arguments.length === 1) {
            return new originalDate(arguments[0]);
        } else {
            return new originalDate(...arguments);
        }
    };

    Date.prototype = originalDate.prototype;
    Date.now = function() {
        return toBeijingTime(originalDate.now()).getTime();
    };
    Date.parse = originalDate.parse;
    Date.UTC = originalDate.UTC;
    Object.defineProperty(Date, 'prototype', { writable: false });
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址