如何解决 "Date" is not instanceof Date 问题?
How to resolve "Date" is not instanceof Date issue?
在对我的 ngx-bootstrap-bsDatepicker 控件和 FormGroup 中的 "Invalid date" 进行故障排除时,我发现我正在使用的对象上的日期值存在问题 (this.ts.startDateTime
).与 new Date()
对象相比时。
08:11:03.723 ts-form.component.ts:240 HZyWZhwowB - this.ts.startDateTime 2019-07-16T20:18:24.9133333
08:11:03.724 ts-form.component.ts:242 HZyWZhwowB - testDate Wed Jul 17 2019 08:11:03 GMT-0400 (Eastern Daylight Time)
08:11:03.724 ts-form.component.ts:244 HZyWZhwowB - this.ts.startDateTime instanceof Date false
08:11:03.725 ts-form.component.ts:246 HZyWZhwowB - testDate instanceof Date true
有问题的 属性 是:
/**
* Gets or sets the start date.
*/
startDateTime: Date;
很容易看出值是不同的。我的对象是从对 WebAPI 控制器的 HTTP 请求接收的数据,打字稿接口是从 WebAPI 中的模型生成的。对于大多数应用程序,对象的日期值在其当前状态下有效。
在 Angular 应用程序中解决此问题并将此值转换为实际日期的最佳方法是什么?
Angular 5 - ASP.NET API
momentjs 存在于应用程序中,但当前未在我的组件中使用。
ts.startDateTime
是一个字符串,因此您必须转换为 Date 对象:
let myDate = new Date(this.ts.startDateTime);
请记住遵守您现有的标准 (ISO 8601),以免破坏上述代码。
在对我的 ngx-bootstrap-bsDatepicker 控件和 FormGroup 中的 "Invalid date" 进行故障排除时,我发现我正在使用的对象上的日期值存在问题 (this.ts.startDateTime
).与 new Date()
对象相比时。
08:11:03.723 ts-form.component.ts:240 HZyWZhwowB - this.ts.startDateTime 2019-07-16T20:18:24.9133333
08:11:03.724 ts-form.component.ts:242 HZyWZhwowB - testDate Wed Jul 17 2019 08:11:03 GMT-0400 (Eastern Daylight Time)
08:11:03.724 ts-form.component.ts:244 HZyWZhwowB - this.ts.startDateTime instanceof Date false
08:11:03.725 ts-form.component.ts:246 HZyWZhwowB - testDate instanceof Date true
有问题的 属性 是:
/**
* Gets or sets the start date.
*/
startDateTime: Date;
很容易看出值是不同的。我的对象是从对 WebAPI 控制器的 HTTP 请求接收的数据,打字稿接口是从 WebAPI 中的模型生成的。对于大多数应用程序,对象的日期值在其当前状态下有效。
在 Angular 应用程序中解决此问题并将此值转换为实际日期的最佳方法是什么?
Angular 5 - ASP.NET API
momentjs 存在于应用程序中,但当前未在我的组件中使用。
ts.startDateTime
是一个字符串,因此您必须转换为 Date 对象:
let myDate = new Date(this.ts.startDateTime);
请记住遵守您现有的标准 (ISO 8601),以免破坏上述代码。