I have just installed the package:
npm install moment-timezone --save
And in angular component I use it like this:
import * as moment from 'moment-timezone';
moment().tz('America/New York');
I guess this adds all timezone data (900+kB) to the vendors bundle and slows down application startup.
How to load it asynchronously, on demand only?
I would really like to use NPM solution, so that the database get's updated automatically when updating npm packages. Nobody would ever remember to update the timezones database manually.
Edit:
@Dimanoid's answer shows how to import moment-timezone from npm without data:
import * as moment from 'moment-timezone/moment-timezone';
And here's how you can include the data as lazy loaded asset:
Modify angular.json -> projects -> yourprojects -> architect -> build -> options:
"assets": [ "src/favicon.ico", "src/assets/i18n", { //add this "glob": "latest.json", "input": "node_modules/moment-timezone/data/packed", "output": "assets/moment-timezone" } ]
request it on demand from assets folder:
import * as moment from 'moment-timezone/moment-timezone'; import { PlatformLocation } from '@angular/common'; export class MyComponent implements OnInit { constructor(private platformLocation: PlatformLocation) { } ngOnInit(){ //you can use @angular/http instead of fetch fetch(this.platformLocation.getBaseHrefFromDOM() + 'assets/moment-timezone/latest.json') .then(time => time.json()) .then(timeZoneData => { moment.tz.load(timeZoneData); }); } }