Skip to content
Advertisement

JavaScript check if timezone name valid or not

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.

JavaScript

Advertisement

Answer

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.

JavaScript

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

JavaScript

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

Advertisement