TypeError: moment().tz is not a function
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
.
for Typescript: Works as of April 2021
import moment from 'moment';
import 'moment-timezone';
cont x = moment.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss ZZ');
cont y = moment().isBetween(
moment.tz('1-1-2021', 'America/Los_Angeles'),
moment.tz('1-1-2021', 'America/Los_Angeles').add(2, 'hours'),
For Node.js, According to the original documentation: moment js documentation
You should do
npm install moment-timezone
Then use it like this
var moment = require('moment-timezone');
moment().tz("America/Los_Angeles").format();
Below code for me...
import moment from 'moment';
import 'moment-timezone';