Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Moment-Timezone with Aurelia/Typescript

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.

Here's my aurelia.json file where I'm loading them from npm:

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

and here's where I'm trying to load them into the file:

import { autoinject } from "aurelia-framework";
import * as moment from "moment";
import * as tz from "moment-timezone";

@autoinject
export class GlobalUtil {

}

Is this the right way to load them? It's not working for me unfortunately.

Now when I try to specify a timezone, I get a "Moment Timezone has no data for America/New_York." Then it console.logs(7am), which is not the correct time.

like image 761
Brandon Avatar asked Sep 12 '16 14:09

Brandon


1 Answers

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!

like image 190
Juliën Avatar answered Jan 04 '23 00:01

Juliën