在Chrome中更改JavaScript日期

Den*_*er9 5 javascript datetime google-chrome

无需更改正在调试的计算机的日期/时间,是否可以通过开发工具或通过在控制台中运行一些自定义JavaScript来设置/更改Google Chrome认为的时间或日期?

IE会new Date()返回您已设置的浏览器认为是的内容,而不是系统的日期。

有很多解决方法,但是调试不同的日期/时间会很方便。

Roh*_*K N 6

使用:

    var fake_date = new Date("October 6, 2016 11:13:00");

    //overriding date function
    Date = function(){return fake_date;};
    var new_date = new Date();
    alert(new_date);
Run Code Online (Sandbox Code Playgroud)

  • 然后对其进行编辑,使其更好。 (8认同)
  • 我所追求的是 Chrome(或任何与此相关的浏览器)中的设置来设置/覆盖“new Date()”返回的内容,而不是实际更改 JavaScript 代码本身。 (2认同)
  • `未捕获的TypeError:Date.now不是函数`:/ (2认同)

Rya*_*kin 5

您需要编写一个函数来包装新日期并返回日期的修改版本。例如:

/**
 * Overwrite Date constructor with configurable current time
 * @param {object} Date         - The native Date object
 * @param {Number} year         - Optional. Default year to this.
 * @param {Number} month        - Optional. Default month to this.
 * @param {Number} day          - Optional. Default day to this.
 * @param {Number} minute       - Optional. Default minute to this.
 * @param {Number} second       - Optional. Default second to this.
 * @param {Number} milliseconds - Optional. Default milliseconds to this.
 */
Date = function (Date, year, month, day, hour, minute, second, milliseconds) {

    function MyDate() {

        // Get arguments passed into new Date()
        var args = Array.prototype.slice.call(arguments);

        // Add null to start
        args.unshift(null);

        // Call new Date() with the original arguments
        var date = new (Function.prototype.bind.apply(Date, args));

        if (typeof year !== 'undefined' && arguments.length === 0) {
            date.setFullYear(year);
        }

        if (typeof month !== 'undefined' && arguments.length === 0) {
            date.setMonth(month);
        }

        if (typeof day !== 'undefined' && (arguments.length === 0 || arguments.length === 2)) {
            date.setDate(day);
        }

        if (typeof hour !== 'undefined' && (arguments.length === 0 || arguments.length === 3)) {
            date.setHours(hour);
        }
        if (typeof minute !== 'undefined' && (arguments.length === 0 || arguments.length === 4)) {
            date.setMinutes(minute);
        }
        if (typeof second !== 'undefined' && (arguments.length === 0 || arguments.length === 5)) {
            date.setSeconds(second);
        }
        if (typeof milliseconds !== 'undefined' && (arguments.length === 0 || arguments.length === 6)) {
            date.setMilliseconds(milliseconds);
        }

        return date;
    }

    MyDate.prototype = Date.prototype;

    return MyDate;

}(Date);
Run Code Online (Sandbox Code Playgroud)

在最后一行,您可以指定覆盖当前时间的值:

}(Date, 1990); // Year
}(Date, 1990, 05); // Year/month
}(Date, 1990, 05, 11); // Year/month/day
}(Date, 1990, 05, 11, 13); // Year/month/day Hour
}(Date, 1990, 05, 11, 13, 05); // Year/month/day Hour:minute
}(Date, 1990, 05, 11, 13, 05, 01); // Year/month/day Hour:minute:second
Run Code Online (Sandbox Code Playgroud)

这样做的好处是任何现有的带有参数的 new Date() 调用仍然可以正常工作:

new Date(2001, 02, 03);
> Mar 03 2001
Run Code Online (Sandbox Code Playgroud)

此外,时间不会冻结,因此秒值将随着正常时钟的增加而增加:

// Year changed to 1990
console.log(new Date());
setTimeout(function(){
    console.log(new Date());
}, 5000);

> Thu Oct 11 1990 17:02:17 GMT+1100 (AUS Eastern Daylight Time)
> Thu Oct 11 1990 17:02:22 GMT+1100 (AUS Eastern Daylight Time)
Run Code Online (Sandbox Code Playgroud)