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'
I was having the same issue, and did everything that acdcjunior suggested, but also had to install the typings for moment-node:
After doing this it worked like a charm.
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
where ever you want to use time-zone in .ts file
That error says the Typescript compiler can't find the
.tz(...)
method onmoment
's type definitions (typings).All you have to do is install
moment-timezone
's typings, so it addstz
tomoment()
.(You usually install typings for every lib you are going to use. The thing is that some, such as angular and moment, have their type definition files embedded in the libs' sources themselves, thus freeing you from the need to install their typings.)
So, as said, just install
moment-timezone
's typings:And everything should work... if you do one thing more:
Rename your configs from
momentzone
tomoment-timezone
(and add the.js
files):That is needed because the name you use here is the name you are going to use in the
import
. And the name you use in theimport
is the name the typescript compiler will use to find the type definitions. And the typings you installed define a module calledmoment-timezone
, notmomentzone
.After that, use:
That should be all.
PS.: In the settings above, your compiler will pick
moment
's typings frommoment
's source ANDmoment-timezone
's typings (thetz
function) from the DefinitelyTyped repository (typings.json).Sometimes, though, they don't play nice. If that happens to you, you'll have to override
moment
's typings from source withmoment
's typings from the DefinitelyTyped repository (typings.json).In other words do:
To install moment-timezone for Angular in 2018:
Install moment.js and moment-timezone
npm install moment moment-timezone @types/moment-timezone --save
Import these modules in your .ts file
import * as moment from 'moment'; import 'moment-timezone';
Github issue: How to resolve the 'tz' build error.
Updated for Angular 4
Install
moment-timezone
on your command line:Import
moment-timezone
in your component:Use it according to docs:
docs
moment docs
moment timezone docs