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');
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.
To change the default time zone, use moment. tz. setDefault with a valid time zone.
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.
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.
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
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
.
Below code for me...
import moment from 'moment'; import 'moment-timezone';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With