我可以通过 Javascript 在浏览器中设置本地时区吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9369972/
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
Can I set the local timezone in my browser via Javascript?
提问by Douglas Mauch
I know I can get the local timezone offset via new Date().getTimeZoneOffset(). But where did Javascript get that information? Is there a way I can set it, so that all future Date objects have the offset I want? I tried searching the DOM in Firebug, but couldn't find anything.
我知道我可以通过 new Date().getTimeZoneOffset() 获取本地时区偏移量。但是 Javascript 从哪里获得这些信息呢?有没有办法设置它,以便所有未来的 Date 对象都具有我想要的偏移量?我尝试在 Firebug 中搜索 DOM,但找不到任何东西。
What I am trying to accomplish is converting epoch times to readable format, but it needs to be in US/Central, no matter what the browser's OS setting. Because I am using US/Central, it's not a fixed difference from GMT. So instead of a bunch of super nasty conversion steps, why can't I just tell Javascript that I'm actually in US/Central?
我想要完成的是将纪元时间转换为可读格式,但无论浏览器的操作系统设置如何,它都需要在美国/中部。因为我使用的是 US/Central,所以它与 GMT 没有固定的区别。因此,为什么我不能告诉 Javascript 我实际上在美国/中部,而不是一堆超级讨厌的转换步骤?
采纳答案by user123444555621
I know I can get the local timezone offset via new Date().getTimeZoneOffset(). But where did Javascript get that information?
我知道我可以通过 new Date().getTimeZoneOffset() 获取本地时区偏移量。但是 Javascript 从哪里获得这些信息呢?
An implementation of ECMAScript is expected to determine the local time zone adjustment.
Is there a way I can set it, so that all future Date objects have the offset I want?
有没有办法设置它,以便所有未来的 Date 对象都具有我想要的偏移量?
No.
不。
So instead of a bunch of super nasty conversion steps, why can't I just tell Javascript that I'm actually in US/Central?
因此,为什么我不能告诉 Javascript 我实际上在美国/中部,而不是一堆超级讨厌的转换步骤?
Have you considered using a library?
你考虑过使用图书馆吗?
回答by igorsantos07
Currently, Moment-Timezone enables us to set the "browser's" default timezone by using moment.tz.setDefault()
.
目前,Moment-Timezone 使我们能够使用moment.tz.setDefault()
.
You'll have to use moment()
instead of Date()
, but this is still a nice upgrade over the weird JS Date object.
你必须使用moment()
而不是Date()
,但这仍然是对奇怪的 JS Date 对象的一个很好的升级。