我正在尝试在我的班级中使用moment-timezone。这是我的打字。
"moment": "github:DefinitelyTyped/DefinitelyTyped/moment/moment.d.ts#a1575b96ec38e916750629839d2110cb96210f89",
"moment-timezone": "github:DefinitelyTyped/DefinitelyTyped/moment-timezone/moment-timezone.d.ts#f8a90040348e83926f44e690b209769c4f88b961"我的导入:
import * as moment from 'moment';
import * as tz from 'moment-timezone';我的用法:
var jun = moment("2014-06-01T12:00:00Z");
jun.tz('America/Los_Angeles').format('ha z');我的错误:
Property 'tz' does not exist on type 'Moment'.发布于 2016-10-05 00:27:02
使用Typescript @types包并通过import * as moment from 'moment-timezone';导入它,您可以使用所有moment方法和成员变量作为moment-timezone导出它们。
我在How to use moment-timezone in Angular2 via SystemJs上有一个使用SystemJS的完整示例
发布于 2017-05-10 18:17:11
请尝试此代码:
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'));
}
}发布于 2016-06-16 17:09:58
我也遇到了同样的问题,并以这种方式解决了它:
键入(ambientDependencies):
"moment": "registry:dt/moment#2.8.0+20160316155526",
"moment-timezone": "github:DefinitelyTyped/DefinitelyTyped/moment-timezone/moment-timezone.d.ts#f8a90040348e83926f44e690b209769c4f88b961"导入:
import * as moment from 'moment';
import 'moment-timezone'用法:
moment("2014-06-01T12:00:00Z")
.tz('America/Los_Angeles')
.format('ha z');所以,基本上我是在moment导入的对象上执行.tz()函数(实际上它并不存在),但是moment-timezone的导入扩展了它的附加函数。
我还使用systemjs-plugin-json来正确地加载json对象,并在moment-timezone库中定义时区。
我希望这能帮到你。
https://stackoverflow.com/questions/37540311
复制相似问题