typescript 如何在 Angular 2 类中使用时刻时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37540311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:32:31  来源:igfitidea点击:

How to use moment-timezone in Angular 2 class?

javascripttypescriptangular

提问by user6403541

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'.

回答by HEMANT PATEL

Please try this code:

请试试这个代码:

import * as moment from 'moment-timezone';

   export class Page1 {  

     public setdate = moment(); // today date
     constructor(){
       this.setdate.tz("Asia/Singapore").format('YYYY-MM-DD HH:mm:ss');
       console.log(this.setdate.tz("Asia/Singapore").format('YYYY-MM-DD HH:mm:ss'));
     }
   }

回答by rynop

Use Typescript @typespackages and import it via import * as moment from 'moment-timezone';You can use all momentmethods and member vars as moment-timezoneexports them.

使用Typescript@types包并通过导入import * as moment from 'moment-timezone';您可以使用所有moment方法和成员变量作为moment-timezone导出它们。

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

我在How to use moment-timezone in Angular2 via SystemJs 中有一个使用 SystemJS 的完整示例

回答by bojanbass

I had the same issue and solved it this way:

我遇到了同样的问题并通过以下方式解决了它:

Typings (ambientDependencies):

类型(环境依赖):

"moment": "registry:dt/moment#2.8.0+20160316155526",
"moment-timezone": "github:DefinitelyTyped/DefinitelyTyped/moment-timezone/moment-timezone.d.ts#f8a90040348e83926f44e690b209769c4f88b961"

Import:

进口:

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

Usage:

用法:

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

So, basically I'm doing .tz() function on moment imported object (which in fact does not exist), but the import of moment-timezone extends it with additional functions.

所以,基本上我正在对时刻导入的对象(实际上不存在)执行 .tz() 函数,但是时刻时区的导入扩展了它的附加功能。

I'm also using systemjs-plugin-json to properly load json object with timezone definitions inside moment-timezone library.

我还使用 systemjs-plugin-json 在 moment-timezone 库中正确加载带有时区定义的 json 对象。

I hope this helps.

我希望这有帮助。

回答by HankCa

I will update this for 2020.

我将在 2020 年更新此内容。

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

import moment from 'moment-timezone';

...

// do this - outside of any class is fine    
moment.tz.add('America/Los_Angeles|PST PDT|80 70|01010101010|1Lzm0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0');


...

someMethod() {
    const m = moment(the_date);
    ...
    const mFormatted = m.tz('America/Los_Angeles').format('YYYY-MM-DDTHH:mm:ssZZ');
}

Timezone defs can be found at https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json. I use this to define my own rather than read them all in.

时区定义可以在https://github.com/moment/moment-timezone/blob/develop/data/packed/latest.json找到。我用它来定义我自己的而不是全部读进去。