Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
5 views
in Web Technology by (20.3k points)

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?

1 Answer

0 votes
by (40.7k points)

Try using the below code:

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

Here, the getTimezoneOffset() method is used to subtract your time from GMT and return the number of minutes. Therefore, if you live in GMT-8, it returns 480.

If you want to put this into hours, divide by 60. 

Note: The sign used in the above code is the opposite of what you need - it's used to calculate GMT's offset from your time zone, not your time zone's offset from GMT. To fix this, you just need to multiply by -1.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...