我有一个角度7应用程序。我正在向我的服务器发送日期。但由于时区的原因,我3个小时后回服务器。而且我了解到解决这个问题的唯一方法就是编写Date.prototype.toJSON
。但是,我不知道有角度,我应该在哪里写代码以及如何? (例如index.html,app.module.ts,...)我在下面写了我的stackblitz示例。
Date.prototype.toJSON = function(key){
//This code return me as string like "25.02.0219 19:48:52"
return this.toLocaleDateString() + ' ' + this.toLocaleTimeString();
}
在您的情况下,您可以在应用程序的主入口点覆盖Date原型是AppModule.ts,以便整个应用程序可以使用它。
App.module.ts
export class AppModule {
constructor() {
this.overrideDate();
}
overrideDate() {
Date.prototype.toJSON = function (key) {
//This code return me as string like "25.02.0219 19:48:52"
return this.toLocaleDateString() + ' ' + this.toLocaleTimeString();
}
}
现在,您可以在组件中使用它。
save() {
console.log(this.myForm.value);
this.http.post("localhost:5000",this.myForm.value).subscribe(result => {});
}
Here is forked stackblitz link
希望这会有所帮助!