Why does moment-timezone fail when I use it in ES6 javascript file?
import moment from 'moment';
// import timezone from 'moment-timezone';
const formatTime = ({timestamp}) => {
const formattedDT = moment.tz(timestamp, 'America/Los_Angeles').format('YYYY-MM-DD HH:mm ZZ');
return formattedDT;
};
Why does moment-timezone fail when I use it in ES6 javascript file?
import moment from 'moment';
// import timezone from 'moment-timezone';
const formatTime = ({timestamp}) => {
const formattedDT = moment.tz(timestamp, 'America/Los_Angeles').format('YYYY-MM-DD HH:mm ZZ');
return formattedDT;
};
Share
Improve this question
edited Jul 18, 2018 at 22:58
Neha M
asked Jul 18, 2018 at 22:50
Neha MNeha M
4631 gold badge5 silver badges14 bronze badges
4
|
3 Answers
Reset to default 13UPD for 2018 (ES6/React), this works when using npm version of moment-tz
import moment from 'moment-timezone';
moment.tz(moment.tz.guess()).zoneAbbr();
The main
field of the package.json
of the moment-timezone
library points to the index.js
file which is a CommonJS module.
It will work as expected if you require the module at the top of your file:
require('moment-timezone');
I ran into this with webpack 4.
My solution was to get the correct instance of "moment".
NOTE: You must also remove any webpack loader for .json that was required in webpack 3.
const moment = require('moment-timezone'); // import 'moment-timezone';
// once we support more regions, the timezone should be allowed to match the user's need
moment.tz.add('America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0');
main
field of thepackage.json
points to./index.js
which seems to be a CommonJS file. Have you triedconst timezone = require('moment-timezone');
? – Tholle Commented Jul 18, 2018 at 22:53require('moment-timezone')
, I'm not sure how moment timezone works. – Tholle Commented Jul 18, 2018 at 23:00import moment from 'moment'; require('moment-timezone'); const formatTime = ({timestamp}) => { const formattedDT = moment.tz(timestamp, 'America/Los_Angeles').format('YYYY-MM-DD HH:mm ZZ'); return formattedDT; };
The require('moment-timezone') worked!! Thanks. – Neha M Commented Jul 18, 2018 at 23:02