Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript check if timezone name valid or not

Tags:

Is there a way to check if timezone name valid or not in JavaScript without using external library?

When user enters timezone name in text field I want to verify whether zone is valid or not?

I know we can do it easily using moment-timezone library. But I don't want to use any extra library. I'm looking for pure JavaScript way.

isValidTimeZone(name) { //return true/false   }  isValidTimeZone('Asia/Colombo'); //returns true isValidTimeZone('America/Los_Angeles'); //returns true isValidTimeZone('MyTimeZone/ME'); //returns false 
like image 530
Pratap A.K Avatar asked May 22 '17 14:05

Pratap A.K


People also ask

How can I get the timezone name 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 offset in JavaScript?

Definition and Usage. getTimezoneOffset() returns the difference between UTC time and local time. getTimezoneOffset() returns the difference in minutes. For example, if your time zone is GMT+2, -120 will be returned.

How do I check my timezone on my browser?

Open DevTools in Chrome -> Open the Console drawer. Click on the three-dotted menu -> Click on More tools -> Sensors. From the Sensors tab, set the location according to your preference and define the specific timezone.

How do I get current timezone in node JS?

you can also use moment(). format('Z') to get the same result. This gives you the current offset in your current timezone but this may vary for part of the year due to DST changes. If in doubt, always use/store UTC date/times plus the timezone identifier and then convert to local as late as possible.


1 Answers

In environments that fully support IANA time zone identifiers in ECMA-402 (ECMAScript Internationalization API), you can try using a time zone in a DateTimeFormat (or in the options of to toLocaleString) and it will throw an exception if it is not a valid time zone. You can use this to test for validity, but only in environments where it is supported.

function isValidTimeZone(tz) {     if (!Intl || !Intl.DateTimeFormat().resolvedOptions().timeZone) {         throw new Error('Time zones are not available in this environment');     }      try {         Intl.DateTimeFormat(undefined, {timeZone: tz});         return true;     }     catch (ex) {         return false;     } }  // Usage: isValidTimeZone('America/Los_Angeles') // true isValidTimeZone('Foo/Bar') // false 

If you cannot be assured of your environment, then the best way would be with moment-timezone

!!moment.tz.zone('America/Los_Angeles') // true !!moment.tz.zone('Foo/Bar') // false 

Of course, you could always extract your own array of time zone names (perhaps with moment.tz.names() and test against that.

like image 83
Matt Johnson-Pint Avatar answered Oct 13 '22 07:10

Matt Johnson-Pint