最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - moment.tz() fails when I use it in ES6react? - Stack Overflow

programmeradmin5浏览0评论

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
  • The main field of the package.json points to ./index.js which seems to be a CommonJS file. Have you tried const timezone = require('moment-timezone');? – Tholle Commented Jul 18, 2018 at 22:53
  • Hi Tholle, Thank you for replying to me. The main field from package.json has this: "main": "", Also, since I am new to react, I was not sure how I would use this timezone const? const formatTime = ({timestamp}) => { const timezone = require('moment-timezone'); const formattedDT = moment.timezone(timestamp, 'America/Los_Angeles').format('YYYY-MM-DD HH:mm ZZ'); return formattedDT; Is that how I would use it? – Neha M Commented Jul 18, 2018 at 22:57
  • 1 You should preferably keep all your imports and requires at the top of the file, outside of any function. You might just have to write require('moment-timezone'), I'm not sure how moment timezone works. – Tholle Commented Jul 18, 2018 at 23:00
  • 1 import 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
Add a comment  | 

3 Answers 3

Reset to default 13

UPD 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');
发布评论

评论列表(0)

  1. 暂无评论