Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript timezone information how to get America/Los_Angeles (or equivalent) [duplicate]

Is there a standard way for a web server to be able to determine a user's timezone within a web page?

Perhaps from an HTTP header or part of the user-agent string?

like image 669
Kevin Dente Avatar asked Aug 01 '08 00:08

Kevin Dente


3 Answers

-new Date().getTimezoneOffset()/60;

The method getTimezoneOffset() will subtract your time from GMT and return the number of minutes. So if you live in GMT-8, it will return 480.

To put this into hours, divide by 60. Also, notice that the sign is the opposite of what you need - it's calculating GMT's offset from your time zone, not your time zone's offset from GMT. To fix this, simply multiply by -1.

Also note that w3school says:

The returned value is not a constant, because of the practice of using Daylight Saving Time.

like image 142
JD Isaacks Avatar answered Oct 06 '22 23:10

JD Isaacks


The most popular (==standard?) way of determining the time zone I've seen around is simply asking the users themselves. If your website requires subscription, this could be saved in the users' profile data. For anon users, the dates could be displayed as UTC or GMT or some such.

I'm not trying to be a smart aleck. It's just that sometimes some problems have finer solutions outside of any programming context.

like image 26
Ishmaeel Avatar answered Oct 07 '22 01:10

Ishmaeel


There are no HTTP headers that will report the clients timezone so far although it has been suggested to include it in the HTTP specification.

If it was me, I would probably try to fetch the timezone using clientside JavaScript and then submit it to the server using Ajax or something.

like image 23
Mads Kristiansen Avatar answered Oct 06 '22 23:10

Mads Kristiansen