javascript 在javascript中设置浏览器窗口的默认时区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11431453/
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
Set default TimeZone of the browser window in javascript
提问by Arun
We are displaying schedules on our webpage which is build on GWT. Client system using different timezone from server and because of that, all the schedules were displaying wrong. Is there a way to set default time zone when we load the page? Like the way we do it in java:
我们在基于 GWT 的网页上显示时间表。客户端系统使用与服务器不同的时区,因此,所有时间表都显示错误。当我们加载页面时,有没有办法设置默认时区?就像我们在java中做的那样:
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Kolkata"));
TimeZone.setDefault(TimeZone.getTimeZone("亚洲/加尔各答"));
Thanks!!!
谢谢!!!
回答by Bergi
No, you can't set the timezone of Date
objects in javascript. Usually you use only UTC and epoch-based timestamps.
不,您不能Date
在 javascript 中设置对象的时区。通常您只使用 UTC 和基于纪元的时间戳。
Only when creating a Date from a string or from year, month etc. the local timezone will be used, you can only get the timezone offset.
只有从字符串或年、月等创建日期时,才会使用本地时区,您只能获取时区偏移量。
Converting a timezone can only be done by re-setting the Hours of the Date object (example described here), creating a date which looks-like having an offset timezone but is just utc.
转换时区只能通过重新设置日期对象的小时数(此处描述的示例)来完成,创建一个看起来像具有偏移时区但只是 utc 的日期。
回答by Alberto Rivera
In case you are using moment.js
for your dates, you can set the default timezone for all newly created moments with:
如果您使用的moment.js
是日期,您可以为所有新创建的时刻设置默认时区:
moment.tz.setDefault(String)
https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/
https://momentjs.com/timezone/docs/#/using-timezones/default-timezone/