当我在ES6 / react中使用它时,moment.tz()失败?

时间:2018-07-18 22:50:57

标签: javascript reactjs ecmascript-6

当我在ES6 javascript文件中使用矩时区时为什么会失败?

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;
};

3 个答案:

答案 0 :(得分:3)

main的{​​{1}}的{​​{1}}字段指向index.js文件,它是CommonJS模块。

如果您在文件顶部需要模块,它将按预期工作:

package.json

答案 1 :(得分:0)

UPD for 2018(ES6 / React),使用npm version of moment-tz时可以使用

import moment from 'moment-timezone';

moment.tz(moment.tz.guess()).zoneAbbr();

答案 2 :(得分:0)

我在webpack 4中遇到了这个问题。

我的解决方案是获取“ moment”的正确实例。

注意:您还必须删除webpack 3中所需的.json的任何webpack加载器。

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');