Override JavaScript's Date object to always use Beijing time (UTC+8)
目前為
此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/493489/1366339/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 });
})();