javascript 有没有办法在 Chrome devtools 中更改时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50849770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:18:47  来源:igfitidea点击:

Is there a way to change your timezone in Chrome devtools?

javascriptgoogle-chrometimezonegoogle-chrome-devtools

提问by iPzard

I've created a tool in JavaScript that determines the current UTC time and checks if another predetermined date has passed yet.

我在 JavaScript 中创建了一个工具,用于确定当前的 UTC 时间并检查另一个预定日期是否已经过去。

I'd like to change my browser to another timezone and see if the tests still pass but I'm having trouble finding a way to do this.

我想将我的浏览器更改为另一个时区,看看测试是否仍然通过,但我找不到方法来做到这一点。

Is there a way to do this in Chrome dev tools? If not, do any other suggestions come to mind?

有没有办法在 Chrome 开发工具中做到这一点?如果没有,是否还有其他建议?

采纳答案by fernandopasik

To do this in Chrome Dev tools you can use the Geolocation sensor emulation. Go to the 3 dotted menu, More Tools and Sensors.

要在 Chrome 开发工具中执行此操作,您可以使用地理定位传感器模拟。转到 3 点菜单,更多工具和传感器。

There's a Geolocation dropdown with some common locations but you can change your preferred one as well. To do that you can choose Other...in that dropdown and enter a custom Timezone ID

有一个包含一些常见位置的 Geolocation 下拉菜单,但您也可以更改您喜欢的位置。为此,您可以在该下拉列表中选择其他...并输入自定义时区 ID

Here's a demo page to test it: https://mathiasbynens.be/demo/timezoneand a video that shows the procedure: https://www.youtube.com/watch?v=pIpN_AuV4AI

这是一个测试它的演示页面:https: //mathiasbynens.be/demo/timezone和一个显示该过程的视频:https: //www.youtube.com/watch?v=pIpN_AuV4AI

Timezone testing in Chrome developer tools

Chrome 开发者工具中的时区测试

回答by Dan Dascalescu

There's no way yetto do this from the DevTools, but I've filed a feature requestto implement a way to override the timezone.

DevTools没有办法做到这一点,但我已经提交了一个功能请求来实现一种覆盖时区的方法。

In the meantime, you can launch the browser with the TZenvironment variable set to the desired timezone nameon Linux/MacOS.

同时,您可以在 Linux/MacOS 上将TZ环境变量设置为所需的时区名称来启动浏览器。

回答by Eli

This worked for me in windows 10. change the system timezone and refresh the page.

这在 Windows 10 中对我有用。更改系统时区并刷新页面。