【发布时间】:2019-12-14 06:36:16
【问题描述】:
我正在使用带有 Asp.Net Core 的 Angular 8。我目前遇到的问题是我无法使用 [(ngModel)] 和日期管道在模态中显示日期。
我想做的是在构造函数中将 Asp.Net Core DateTime 转换为 Angular Date 格式。
这是我的代码。 Created、StartDate 和 EndDate 都是 Date。
export class Content {
Id: string;
CompanyId: string;
Created: Date;
Name: string;
Url: string;
Instructions: string;
Tags: string;
StartDate: Date;
EndDate: Date;
Deleted: Boolean;
Pause: Boolean;
Published: Boolean;
constructor(contentResponse: any) {
this.Id = contentResponse.id;
this.CompanyId = contentResponse.companyId;
this.Created = contentResponse.Created;
this.Name = contentResponse.name;
this.Url = contentResponse.url;
this.Instructions = contentResponse.instructions;
this.Tags = contentResponse.tags;
this.StartDate = contentResponse.startDate;
this.EndDate = contentResponse.endDate;
this.Deleted = contentResponse.deleted;
this.Pause = contentResponse.pause;
this.Published = contentResponse.published;
}
}
从 console.log 中,我可以看到以下日期格式。
来自 API 的 JSON
创建:“2019-08-03T21:26:51.7981863”
结束日期:“2020-12-31T00:00:00”
开始日期:“2019-07-15T00:00:00”
使用当前构造函数转换后
已创建:未定义
结束日期:“2020-12-31T00:00:00”
开始日期:“2019-07-15T00:00:00”
有谁知道如何在构造函数中转换 this 以便 Angular 可以显示这些信息并使用它?
非常感谢任何帮助。谢谢!
【问题讨论】:
-
在everyDate试试这个--->
new Date("2019-08-03T21:26:51.7981863"); -
@sagat 谢谢!我在 EndDate 上尝试了这条确切的路线,看看会发生什么。这是结果。 EndDate: Sat Aug 03 2019 21:26:51 GMT-0500 (Central Daylight Time) {} 我看到一个 proto: 对象以及各种嵌套对象。此处不显示 [(ngModel)]="formModel.EndDate"。
标签: angular asp.net-core