[JavaScript] Sample Code for Timezone String Validation with moment.tz.zone

Tadashi Shigeoka ·  Thu, September 26, 2019

I’ll introduce sample code for validating timezone strings with moment.tz.zone from Moment Timezone.

Moment-Timezone

Prerequisites

Moment Timezone Zone Object | Moment Timezone

Validate Timezone Strings with moment.tz.zone

Here’s sample code for validating timezone string format using moment.tz.zone:

It would be good to define a method like validateTimezoneFormat for use.

const moment = require('moment-timezone');

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

!!moment.tz.zone('Asia/Tokyo') // true

!!moment.tz.zone('Asia/Toky') //false

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


const validateTimezoneFormat = (tz) => {
  return !!moment.tz.zone(tz);
};

That’s all from the Gemba.