Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Include moment-timezone in Angular 2 App

I realize, this question is repeated but previous once does not provide apt answer moment package was already installed

1.Installed package

npm install moment-timezone --save

Inside node_modules directory
|
|--moment
|--moment-timezone

directories present

2. Index.html included script

<script src="node_modules/moment-timezone/moment-timezone.js"></script>

System.config.js

  var map = {
    'moment':             'node_modules/moment',
    'momentzone':         'node_modules/moment-timezone'
  };
  var packages = {
    'moment':             { defaultExtension: 'js' },
    'momentzone':         { defaultExtension: 'js' }
  };

3.Inside component.ts file

import * as moment from 'moment/moment';

export class TimeComponent implements OnInit{
   ngOninit(){
         console.log(moment("2014-06-01T12:00:00Z").tz('America/Los_Angeles').format('ha z'));

   }
}

What should be imported to prevent error Property tz does not exist on type 'Moment'

like image 422
Sandeep Chikhale Avatar asked Jul 30 '16 14:07

Sandeep Chikhale


People also ask

How do I add a moment to my timezone?

However, moment-timezone adds a moment.tz() that you can use to create a new moment object with a custom timezone. For example, here's how you can create a moment object representing the current time in Longyearbyen, Norway. // 'Tue Jun 08 2020 23:13:16 GMT+0200' moment.tz('Arctic/Longyearbyen'). toString();

How do I use moment-timezone in typescript?

Use Typescript @types packages and import it via import * as moment from 'moment-timezone'; You can use all moment methods and member vars as moment-timezone exports them. Show activity on this post. Show activity on this post.

How do I get moments from date and time zone?

You can use moment.tz() to get timezone full name. It will return undefined if timezone is not set.

Is moment-timezone deprecated?

This format is deprecated and will be removed in future.


3 Answers

This might help.

Run Following command for angular-cli or npm install.

sudo npm install moment moment-timezone --save
npm install @types/moment @types/moment-timezone --save-dev

for npm systemjs.config.js

   map: {
        'moment': 'npm:moment',
        'moment-timezone': 'npm:moment-timezone/builds'
      }
packages: { 
      'moment': {
          main: './moment.js',
          defaultExtension: 'js'
       },
      'moment-timezone': {
        main: './moment-timezone-with-data-2010-2020.min.js',
        defaultExtension: 'js'
      }
}

where ever you want to use time-zone in .ts file

import * as moment from 'moment-timezone';

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>`,
})

export class AppComponent {    
  name = 'Angular';
  jun = moment();// creating obj.
  constructor() {
    this.jun.tz('America/Los_Angeles').format('hh : mm : ss a z');
    console.log(this.jun.tz('America/Los_Angeles').format('hh : mm : ss a z'));
    console.log(moment.tz.names()); // for all time zone.
  }
like image 73
vishvas chauhan Avatar answered Oct 06 '22 13:10

vishvas chauhan


Updated for Angular 4

Install moment-timezone on your command line:

npm i -S moment-timezone

Import moment-timezone in your component:

import * as moment from 'moment-timezone';

Use it according to docs:

this.time = moment().tz('America/New_York').format('HH:mm:ss z')

16:20:42 EDT

docs

moment docs

moment timezone docs

note: moment was intentionally not installed or imported as it is a dependency of moment-timezone.

like image 40
Matt Avatar answered Oct 06 '22 11:10

Matt


To install moment-timezone for Angular in 2018:

  1. Install moment.js and moment-timezone

    npm install moment moment-timezone @types/moment-timezone --save

  2. Import these modules in your .ts file

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

Github issue: How to resolve the 'tz' build error.

like image 7
vulp Avatar answered Oct 06 '22 12:10

vulp