Change browser time to test return value of Date()?

后端 未结 3 975
無奈伤痛
無奈伤痛 2021-02-18 21:17

Is there any way to change the browser\'s time without manipulating the system clock?

相关标签:
3条回答
  • 2021-02-18 21:40

    The browser doesn't really "have time", it gets its time from the system clock. Of course, if you want to do something particularly nasty, you could override the Date functions.

    Date.prototype.getTime = function() { return 1 };
    (new Date).getTime(); // 1
    

    So if you wanted to set the time to 1am November 4th 1989, you'd first find the time value:

    (new Date('1989-11-04T01:00:00')).getTime() // Returns 626144400000
    

    Then mock it in browser:

    Date.prototype.getTime = function() { return 626144400000 };
    
    0 讨论(0)
  • 2021-02-18 21:46

    No. The browser doesn't have a time. The system does.

    0 讨论(0)
  • 2021-02-18 21:52

    You can run the browser in a virtual machine (VMWare/VirtualPC/etc.) and change the time of the OS in the VM.

    0 讨论(0)
提交回复
热议问题