我将一个包含两个日期的对象从Angular 6发送到ASP.NET Core Web API方法。 Angular方面的日期是正确的,但是当它们在Web API方法中被接收时,它们比从Angular应用程序发送的日期落后一天。
startSprint(id: number, SprintDates: any): Observable<any> {
this.sprintId = id.toString();
return this.http.patch<any>(this.baseUrl + 'setSprintDates/' + this.sprintId,
SprintDates);
}
上述方法将日期对象发送到Web API方法。下面的方法是接收该对象的Web API方法,但是当我调试并将光标悬停在sprintDatesDto
对象上时,收到的值比Angular发送的值晚了一天。
[HttpPatch("setSprintDates/{sprintId}")]
public async Task<IActionResult> SetSprintDates(string sprintId, SprintDatesDto sprintDatesDto)
{
// Business login related code
return StatusCode(200);
}
这是SprintDatesDto
类:
namespace AgileFootPrints.API.Dtos
{
public class SprintDatesDto
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
}
这是我在angular Typescript文件中创建的对象:
sprinDates = {
startDate: Date,
endDate: Date
};
接下来,在Html文件中,我使用了ngModel绑定到sprintDates对象属性:
<div class="row">
<div class="col-md-7 w-100 ">
<mat-form-field class="example-full-width">
<input
matInput
[(ngModel)]="sprinDates.startDate"
[min]="minDate"
[max]="maxDate"
[matDatepicker]="startsAt"
placeholder="Starts At"
/>
<mat-datepicker-toggle matSuffix [for]="startsAt"></mat-datepicker-toggle>
<mat-datepicker #startsAt></mat-datepicker>
</mat-form-field>
</div>
<br />
</div>
<div class="row">
<div class="col-md-7 w-100">
<mat-form-field class="example-full-width">
<input
[disabled]="sprinDates.startDate === undefined"
matInput
[(ngModel)]="sprinDates.endDate"
[min]="sprinDates.startDate"
[max]="maxDate"
[matDatepicker]="endsAt"
placeholder="Ends At"
/>
<mat-datepicker-toggle matSuffix [for]="endsAt"></mat-datepicker-toggle>
<mat-datepicker #endsAt></mat-datepicker>
</mat-form-field>
</div>
</div>
问题是它的日期被Json解析,默认情况下是角度,它会增加与UTC相比的本地时间(例如:如果你是午夜的Gmt + 1,它将转换为昨天晚上11点的UTC),你是什么需要做的是创建一个文件,覆盖MomentDateAdapter以忽略此转换并以UTC格式创建所选时间,如下所示:
import { Inject, Injectable, Optional } from '@angular/core';
import { MAT_DATE_LOCALE } from '@angular/material';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { Moment } from 'moment';
import * as moment from 'moment';
@Injectable()
export class MomentUtcDateAdapter extends MomentDateAdapter {
constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) {
super(dateLocale);
}
createDate(year: number, month: number, date: number): Moment {
// Moment.js will create an invalid date if any of the components are out of bounds, but we
// explicitly check each case so we can throw more descriptive errors.
if (month < 0 || month > 11) {
throw Error(
`Invalid month index "${month}". Month index has to be between 0 and 11.`
);
}
if (date < 1) {
throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
}
const result = moment.utc({ year, month, date }).locale(this.locale);
// If the result isn't valid, the date must have been out of bounds for this month.
if (!result.isValid()) {
throw Error(`Invalid date "${date}" for month with index "${month}".`);
}
return result;
}
}
接下来只需将以下提供程序和此新文件导入到app.module或angular material模块的导入中(如果有)
@NgModule({
providers: [
{ provide: MAT_DATE_LOCALE, useValue: 'en-GB' },
{ provide: MAT_DATE_FORMATS, useValue: MAT_MOMENT_DATE_FORMATS },
{ provide: DateAdapter, useClass: MomentUtcDateAdapter },
],
imports: [
MomentUtcDateAdapter,
... rest of your imports]