How to import Moment-Timezone with Aurelia/Typescript

只谈情不闲聊 提交于 2019-12-01 19:05:09

Maybe I can help a bit with this. In order to give decent instructions, I'll add step-by-step instructions for the entire chain of adding moment/moment-timezone to an Aurelia CLI app.

Install moment.js

  • npm install --save moment
  • typings install dt~moment --global --save

Install moment-timezone

  • npm install --save moment-timezone
  • typings install dt~moment-timezone --global --save

Note: I'm assuming you want to use TypeScript. If not, skip the second steps from the installation instructions above.

Next, configure both in the "dependencies" section of aurelia.json:

{
    "name": "moment",
    "path": "../node_modules/moment",
    "main": "moment"
},
{
    "name": "moment-timezone",
    "path": "../node_modules/moment-timezone",
    "main": "moment-timezone",
    "deps": ["moment"]
}

Finally, let's wrap this up with a simple example on how to actually use this:

import * as moment from 'moment';
import 'moment-timezone';

export class App {
  message: string;

  constructor() {
    // basic example of using moment().tz
    this.message = moment.tz("2014-06-01 12:00", "Europe/Amsterdam").format();
  }

}

That should be it! So far so good, at least for the configuration part. But, as your question already indicates, you may receive an error in the trend of:

Moment Timezone has no data for Europe/Amsterdam. See http://momentjs.com/timezone/docs/#/data-loading/

This means that, by default, moment-timezone has no knowledge of that specific timezone. As the link from the error message mentions, you have to add it yourself. For example like:

moment.tz.add([
    'America/Los_Angeles|PST PDT|80 70|0101|1Lzm0 1zb0 Op0',
    'Europe/Amsterdam|EST EDT|50 40|0101|1Lz50 1zb0 Op0'
]);

Add as much timezones as you'd like to this array. Or download/use a predefined bundle from the momentjs website (see link above).

Hope this helps!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!