Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get time zone of city or country in javascript

Tags:

javascript

I use getTimezoneOffset() to get the offset in minutes for a given date object. Is is possible to use javascript to get timezone offset of a city or a country?

for example:

var offset = getCityOffset("Miami"); // returns -240
like image 827
sanjihan Avatar asked May 03 '16 10:05

sanjihan


People also ask

How do I find my timezone in JavaScript?

To get the current browser's time zone, you can use the getTimezoneOffset() method from the JavaScript Date object. The getTimezoneOffset() returns the time difference, in minutes, between UTC time and local time.

What is timezone 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.

How do you find time zones with time?

The . getTimezoneOffset() method should work. This will get the time between your time zone and GMT. You can then calculate to whatever you want.

How does JavaScript handle different time zones?

You can't change the Date object's behavior to get it to use a different time zone just by adding or subtracting an offset. It will still use the local time zone of where it runs, for any function that requires a local time, such as . toString() and others.


1 Answers

No, there is nothing built-in to javascript which allows you to get the number of minutes to offset for specific time zones/cities.

getTimeZoneOffset works for the current browser's settings

MomentJS Timezone extensions has some of this sort of functionality, which is of course reliant on the MomentJS library.

If you have access to Lat/Long values, then google provide a timezone API

like image 124
Jamiec Avatar answered Sep 24 '22 20:09

Jamiec