Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use moment-timezone in Angular 2 class?

I am trying to use moment-timezone in my class. This is my typings.

"moment": "github:DefinitelyTyped/DefinitelyTyped/moment/moment.d.ts#a1575b96ec38e916750629839d2110cb96210f89",
"moment-timezone": "github:DefinitelyTyped/DefinitelyTyped/moment-timezone/moment-timezone.d.ts#f8a90040348e83926f44e690b209769c4f88b961"

My import:

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

My usage:

var jun = moment("2014-06-01T12:00:00Z");
jun.tz('America/Los_Angeles').format('ha z');

My error:

Property 'tz' does not exist on type 'Moment'.
like image 212
user6403541 Avatar asked May 31 '16 08:05

user6403541


People also ask

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 you use moment-timezone?

To use moment-timezone, you will need [email protected]+ , moment-timezone. js , and the moment-timezone data. For convenience, there are builds available on momentjs.com/timezone/ with all the zone data or a subset of the data. moment-timezone-with-data.

How do you get timezone from moment date?

You can use moment.tz() to get timezone full name. It will return undefined if timezone is not set. Beside, you can use moment. format('zz') to get short timezone name.

Does moment use UTC by default?

utc(Date); By default, moment parses and displays in local time. If you want to parse or display a moment in UTC, you can use moment.


1 Answers

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.

I have a complete example using SystemJS at How to use moment-timezone in Angular2 via SystemJs

like image 146
rynop Avatar answered Oct 02 '22 16:10

rynop