0

javascript

var get_tz = function(country_code){
 var tz = new Date(currentUtcTime.toLocaleString('en-US', { timeZone: country_code}));
 return (tz.getHours()<10?"0"+tz.getHours():tz.getHours())+":"+(tz.getMinutes()<10?"0"+tz.getMinutes():tz.getMinutes());
}

MDN suggests the above method will be supported by most browser except mobile phone. I tried to run the page on my iPhone, and it worked as expected. Maybe I have the latest OS.

I have my doubts and wonder if there is a better way or new way to approach timezone problem.

Read

"Date.prototype.toLocaleString" from MDN

How to check if the DST (Daylight Saving Time) is in effect and if it is what's the offset?

Convert date to another timezone in JavaScript

iana

Community
  • 1
  • 1
roger
  • 1,225
  • 2
  • 17
  • 33
  • What are you actually trying to do? Date instances have access to system settings so can determine the current time zone offset using [*Date.prototype.getTimezoneOffset*](http://www.ecma-international.org/ecma-262/7.0/index.html#sec-date.prototype.gettimezoneoffset). The "*country_code*" argument is actually an IANA timezone code. – RobG Apr 18 '17 at 05:38
  • Yes `"country_code"` is IANA timezone code. I try to create a function that takes some input, then it returns date time for the given country. It must work on IE7 onward and mobile, but the `toLocaleString` works with only firefox, chrome and IE edge. – roger Apr 18 '17 at 20:36
  • 1
    Then you will need to use a library, IE 7 certainly doesn't have support for time zones with *toLocaleString* (nor would I expect any browser from that era to support it as it wasn't part of ECMA-262 back then and ECMA-402 didn't exist, the first edition was December 2012). – RobG Apr 18 '17 at 23:45
  • Which lib would you recommend for this situation? – roger Apr 18 '17 at 23:56
  • If you want to get the offset for particular time zones (identified by IANA timezone code) at particular dates and times, then [*Moment Timezone*](https://momentjs.com/timezone/) is a reasonable candidate. – RobG Apr 19 '17 at 01:03

0 Answers0