Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set the local timezone in my browser via Javascript?

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?

like image 658
Douglas Mauch Avatar asked Feb 20 '12 23:02

Douglas Mauch


People also ask

How do I get current time zone in JavaScript?

The JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, local timezone is behind the UTC and if it is negative, the local timezone if ahead of UTC.

Does JavaScript default to UTC?

The JavaScript Date is always stored as UTC, and most of the native methods automatically localize the result.

What is default timezone in JavaScript?

Sometimes, we may want to initialize a JavaScript date to a particular time zone. The default time zone is UTC.


2 Answers

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?

like image 105
user123444555621 Avatar answered Oct 13 '22 05:10

user123444555621


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.

like image 21
igorsantos07 Avatar answered Oct 13 '22 06:10

igorsantos07