How to import Moment-Timezone with Aurelia/Typescript

后端 未结 1 1065
予麋鹿
予麋鹿 2021-01-19 02:52

I\'ve imported momentjs properly. It\'s working fine, but when I go to try to import moment-timezone, I can\'t get it to work. I don\'t have access to any functions.

相关标签:
1条回答
  • 2021-01-19 03:00

    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!

    0 讨论(0)
提交回复
热议问题