Can I set the local timezone in my browser via Jav

2019-02-21 15:55发布

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.

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?

3条回答
爷的心禁止访问
2楼-- · 2019-02-21 16:16

I know I can get the local timezone offset via new Date().getTimeZoneOffset(). But where did Javascript get that information?

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?

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?

Have you considered using a library?

查看更多
我只想做你的唯一
3楼-- · 2019-02-21 16:17

Currently, Moment-Timezone enables us to set the "browser's" default timezone by using 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.

查看更多
ら.Afraid
4楼-- · 2019-02-21 16:30

I realize this is an old post, but momentJS is a powerful javascript library to manipulate date/time objects

查看更多
登录 后发表回答