I'm trying to write a function in Javascript to get the local time of another city, for example, my location is CA, USA, then I want to get the local time in London, UK. As far as I know, both CA and London would have daylight saving time. And they don't start at the same date, and even the DST doesn't start at the same date in London every year. How could I achieve this in Javascript? I searched and read a lot of posts online, but still can not figure out how to do that.
Asked
Active
Viewed 935 times
0
-
Does this answer your question? [how to get time zone of city or country in javascript](https://stackoverflow.com/questions/37001869/how-to-get-time-zone-of-city-or-country-in-javascript) – Ahmed Ghonim May 03 '20 at 05:38
-
Thanks Ahmed. I studied the google timezone API, but it seems it's not free. – Ames ISU May 03 '20 at 05:44
1 Answers
0
You can do something like this:
const time = new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
Here are the list of all the timezones: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

James
- 341
- 3
- 8
-
Does that take care of the DST? Another question is how to find the time zone of a city, for example, Bedford, UK? – Ames ISU May 03 '20 at 05:47
-
You can check if the daylight saving time is in effect by doing something like this `return this.getTimezoneOffset() < this.stdTimezoneOffset();` Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset And unfortunately, I dont think you can get a time for a specific city directly. You will need to manually input the data for the cities. – James May 03 '20 at 05:50
-
OK, thanks. Sorry another question: what about the mountain time like some cities in US? Is there any way to sort that out? – Ames ISU May 03 '20 at 05:51
-
No problem :) There isn't a symbol for mountain time, but you can calculate it. Mountain time is basically current UTC - 7 hours – James May 03 '20 at 06:07
-
When you say "And unfortunately, I dont think you can get a time for a specific city directly. You will need to manually input the data for the cities." Does that mean I need to find the time zone name for a city? I googled the city "Bedford, UK", but I cannot figure out which time zone it corresponds to in the link: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones. Can you illustrate how to do this? Thanks a lot! – Ames ISU May 03 '20 at 06:16
-
I googled "Beford UK Timezone" and its GMT +1. On the list "Africa/Brazzaville" is GMT+1. Another option is just get the UTC date and add 1 hour. – James May 03 '20 at 06:32
-
I think I understand much better now. A final question (hopefully) is how do i know whether a city (not my city) is using DST? – Ames ISU May 03 '20 at 07:10
-
@AmesISU Sorry for the late reply. There isn't a way to list all the cities that uses DST, but you can always check if a timezone is using DST by the code I've written above `return this.getTimezoneOffset() < this.stdTimezoneOffset();` Hope this helps :) – James May 03 '20 at 15:37