Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: moment().tz is not a function

When testing using jasmine, I am getting this error.

TypeError: moment.tz is not a function

My code that I try to test is

let myDate = moment().tz(undefined, vm.timeZone).format('YYYY-MM-DD');  
like image 762
Sanath Avatar asked Oct 31 '17 23:10

Sanath


People also ask

What is moment TZ ()?

The moment#tz mutator will change the time zone and update the offset. moment("2013-11-18").tz("America/Toronto").format('Z'); // -05:00 moment("2013-11-18").tz("Europe/Berlin").format('Z'); // +01:00. This information is used consistently in other operations, like calculating the start of the day.

How do I set moment time zone?

To change the default time zone, use moment. tz. setDefault with a valid time zone.

How do I use moment-timezone in typescript?

Use Typescript @types packages and import it via import * as moment from 'moment-timezone'; You can use all moment methods and member vars as moment-timezone exports them. Show activity on this post. Show activity on this post.

Does moment-timezone include moment?

Moment-timezone is a separate npm module from moment, but moment-timezone depends on moment under the hood. So you can install moment-timezone by itself, or both moment and moment-timezone. Once you install moment-timezone, you can require() it in and use it like you would use moment.


Video Answer


2 Answers

Fix

If you're using Node.js, you may accidentally be using

const moment = require('moment'); //moment

instead of

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

Also, make sure you have installed moment-timezone with

npm install moment-timezone --save

Explanation

The bug of requiring moment without timezones could occur by installing moment with require('moment'), later deciding to npm install moment-timezone, and then forgetting to update the require.

like image 147
Matt Goodrich Avatar answered Oct 06 '22 01:10

Matt Goodrich


Below code for me...

import moment from 'moment'; import 'moment-timezone'; 
like image 22
Purushottam Sadh Avatar answered Oct 06 '22 01:10

Purushottam Sadh