JavaScript Detect an invalid timezone with momentjs

Is there a way I can detect an invalid timezone with momentjs? If I do this:

const m = moment.tz("01/01/2019 5:30pm", "MM/DD/YYYY h:mma", true, "invalid timezone");
m.isValid(); //true

Is there any way to make isValid() return false, or is there some other way to detect that the timezone is invalid?

It logs the following: Moment Timezone has no data for invalid timezone. See http://momentjs.com/timezone/docs/#/data-loading/ but how can I tell programatically?

Answer:1

If you're working with a fully created Moment object, then the answer given by Jaromanda X is the way to go. However, if you're just testing for the time zone name being valid (and loaded into moment), then you don't need to construct a Moment object to do that. Just test the time zone name itself.

if (!moment.tz.zone('FooBarTimeZone')) {
    console.log('Invalid time zone');
} else {
     console.log('Valid time zone');
}

Note that this works because the moment.tz.zone function returns either a Zone object (which like any object is truthy), or null (which is falsey).

Also note that there was originally a moment.tz.zoneExists function, which still works - but will now give a deprecation message, describing using the above technique instead.

Answer:2

I currently have a list of checkboxes in my webapp. I want to show the order in which the checkboxes have been checked. So I wrote the code below. $scope.updateNumbers = function(id, checked, inputs) ...

I currently have a list of checkboxes in my webapp. I want to show the order in which the checkboxes have been checked. So I wrote the code below. $scope.updateNumbers = function(id, checked, inputs) ...

So I have this: const Link = styled.a` color: blue; `; <Wrapper> <Link href="/">Hover Change</Link> <Wrapper> I want to put a hover effect on just the Link element ...

So I have this: const Link = styled.a` color: blue; `; <Wrapper> <Link href="/">Hover Change</Link> <Wrapper> I want to put a hover effect on just the Link element ...

  1. hover styled component
  2. hover state styled components
  3. button hover styled components
  4. styled component hover parent
  5. styled component hover react
  6. styled component hover effect
  7. hover styled components

I want to add an image to the legend of a pie in Highcharts Yii2. See example but I cannot write it into my PHP widget in the labelFormat section. <? = \dosamigos\highcharts\HighCharts::widget([ ...

I want to add an image to the legend of a pie in Highcharts Yii2. See example but I cannot write it into my PHP widget in the labelFormat section. <? = \dosamigos\highcharts\HighCharts::widget([ ...

The new spread syntax can be used to elegantly merge several objects in the following way: const person = { name: 'David Walsh', gender: 'Male' }; const tools = { computer: 'Mac', editor: 'Atom' }; ...

The new spread syntax can be used to elegantly merge several objects in the following way: const person = { name: 'David Walsh', gender: 'Male' }; const tools = { computer: 'Mac', editor: 'Atom' }; ...