Dan
Dan

Reputation: 11069

Using Javascript to detect locale/timezone

Javascript provides us nice functions like toLocaleString, toLocaleTimeString etc. But I'm wondering - what is the right way to actually find out what the user's locale is, or at least their current time zone?

The use case I have in mind is I'm asking the user to input a date and time, and I want to construct a Date object from that but I need to know what time zone to use, since there's no standard function for it.

Upvotes: 1

Views: 906

Answers (2)

Adam L Davis
Adam L Davis

Reputation: 279

Use this to get timezone: https://bitbucket.org/pellepim/jstimezonedetect

Upvotes: 1

kan
kan

Reputation: 28951

As far as I remember, the JavaScript doesn't have any timezone information, it has only current timezone offset (which is changing during DST).

So, you could use new Date(2011, 10, 16, 15, 2, 0, 0).getTime() to take current unix millseconds to determine precise instant of time using user's current timezone. However, if you need then convert it between different timezones, you should ask user's timezone explicitly and do conversions on server side.

Upvotes: 1

Related Questions