Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine a user's timezone

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 373
Kevin Dente Avatar asked Aug 01 '08 00:08

Kevin Dente


People also ask

How do I find the timezone of a user?

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.

What is user time zone?

The user time zone is a client-specific time zone that can be defined for the user time and user date of each individual user in the user master record. It is contained in the system field sy-zonlo.

How do I find the timezone of my server?

Checking Your Current TimezoneThe currently configured timezone is set in the /etc/timezone file. To view your current timezone you can cat the file's contents. Another method is to use the date command. By giving it the argument +%Z , you can output your system's current time zone name.


2 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 71
JD Isaacks Avatar answered Nov 10 '22 12:11

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 25
Ishmaeel Avatar answered Nov 10 '22 11:11

Ishmaeel