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'
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();
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.
You can use moment.tz() to get timezone full name. It will return undefined if timezone is not set.
This format is deprecated and will be removed in future.
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.
}
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
moment docs
moment timezone docs
note:
moment
was intentionally not installed or imported as it is a dependency ofmoment-timezone
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With