// Save the original `Date` function
const OriginalDate = Date;
// Replace it with our own
Date = function Date(...args) {
// Called via `new`?
if (!new.target) {
// No, just pass the call on
return OriginalDate(...args);
}
// Determine what constructor to call
const ctor = new.target === Date ? OriginalDate : new.target;
// Called via `new`
if (args.length !== 0) {
// Date constructor arguments were provided, just pass through
return Reflect.construct(ctor, args);
}
// It's a `new Date()` call, mock the date we want; in this
// example, Jan 1st 2000:
return Reflect.construct(ctor, [2000, 0, 1]);
};
// Make our replacement look like the original (which has `length = 7`)
// You can't assign to `length`, but you can redefine it
Object.defineProperty(Date, "length", {
value: OriginalDate.length,
configurable: true
});
字符串
// Save the original `Date` function
const OriginalDate = Date;
// Replace it with our own
Date = function Date(...args) {
// Called via `new`?
if (!new.target) {
// No, just pass the call on
return OriginalDate(...args);
}
// Determine what constructor to call
const ctor = new.target === Date ? OriginalDate : new.target;
// Called via `new`
if (args.length !== 0) {
// Date constructor arguments were provided, just pass through
return Reflect.construct(ctor, args);
}
// It's a `new Date()` call, mock the date we want; in this
// example, Jan 1st 2000:
};
// Make our replacement look like the original (which has `length = 7`)
// You can't assign to `length`, but you can redefine it
Object.defineProperty(Date, "length", {
value: OriginalDate.length,
configurable: true
});
console.log("new Date()", new Date());
console.log("new Date(2021, 7, 3)", new Date(2021, 7, 3));
// Save the original `Date` function
const OriginalDate = Date;
const fakeDateArgs = [2022, 5, 3]; // beware month is 0 based
let fakeDate;
// Replace it with our own
Date = function Date(...args) {
// Called via `new`?
if (!new.target) {
// No, just pass the call on
return OriginalDate(...args);
}
// Determine what constructor to call
const ctor = new.target === Date ? OriginalDate : new.target;
// Called via `new`
if (args.length !== 0) {
// Date constructor arguments were provided, just pass through
return Reflect.construct(ctor, args);
}
// It's a `new Date()` call, mock the date we want; in this
fakeDate = Reflect.construct(ctor, fakeDateArgs);
return fakeDate;
};
// Make our replacement look like the original (which has `length = 7`)
// You can't assign to `length`, but you can redefine it
Object.defineProperty(Date, "length", {
value: OriginalDate.length,
configurable: true
});
Object.defineProperty(Date, "now", {
value: () => fakeDate.getTime(),
configurable: true
});
((originalDate) => {
// Function to mock the Date object
function MockDate(...args) {
if (args.length) {
return new originalDate(...args);
}
// Default date: January 1, 2024
return new originalDate(2024, 0, 1);
}
MockDate.prototype = originalDate.prototype;
MockDate.now = () => new MockDate().getTime();
MockDate.UTC = originalDate.UTC;
MockDate.parse = originalDate.parse;
// Override the global Date object
Date = MockDate;
})(Date);
4条答案
按热度按时间vltsax251#
可以用你自己的函数来替换
Date
函数,以提供你想要的结果,但是在页面使用它之前这样做会很棘手,除非你写一个浏览器扩展。基本位是(见注解):
字符串
型
mw3dktmi2#
谢谢你@t-j-crowder,这是我迄今为止找到的最好的解决方案.我对它进行了一些定制,所以Date.now也可以工作。
另外一个建议是,在脚本的开头放置断点,在控制台中运行代码并恢复执行,以获得最佳的日期一致性。
我的修改:
字符串
uqjltbpv3#
您可以在加载内容之前使用此命令修改内容:https://developer.chrome.com/docs/extensions/reference/webRequest/
有一个我没有使用过的扩展可能可以做到这一点:https://chrome.google.com/webstore/detail/page-manipulator/mdhellggnoabbnnchkeniomkpghbekko?hl=en
ukxgm1gy4#
下面是在浏览器中模拟Date对象的实现。Month总是从零开始,默认日期设置为2024年1月1日,可以更改。这最好仅用于测试目的:
字符串
可使用以下设备进行测试:
型