我有一个Angular 8应用程序。我正在使用资料的日期选择器。
但是,如果我填写一个日期,则会调用get api:
searchFor(filterRegistration: FilterByRegistrationDTO) {
// tslint:disable-next-line: max-line-length
console.log(
this.participantService.filterParticipantsByRegistration(1, "Invited", this.startDate.toString()).subscribe(result => {
console.log(this.startDate.toString());
console.log(result);
})
);
模板如下:
<div>
<mat-form-field class="search-field-input">
<input matInput [matDatepicker]="picker1" placeholder="start datum" [(ngModel)]="startDate" />
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker #picker1></mat-datepicker>
</mat-form-field>
</div>
因此在服务器端,日期将显示为:jjjj-MM-dd。
但是在客户端,日期必须显示为:dd-MM-jjjj。
但是如果我调用api调用:
console.log(
this.participantService.filterParticipantsByRegistration(1, "Invited", this.startDate.toString()).subscribe(result => {
console.log(this.startDate.toString());
console.log(result);
})
);
然后我会收到此错误:
core.js:12584 ERROR
HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: "https://dev-engine.mijnhep.nl/api/medical/organisa…GMT%2B0200%20(Central%20European%20Summer%20Time)", ok: false, …}
error:
Start: ["The value 'Mon Oct 07 2019 12:58:46 GMT+0200 (Central European Summer Time)' is not valid for Start."]
__proto__: Object
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure response for https://dev-/api/medical/organisation/1/Participant/filter-by-registration?Filter=Invited&Start=Mon%20Oct%2007%202019%2012:58:46%20GMT%2B0200%20(Central%20European%20Summer%20Time): 400 Bad Request"
name: "HttpErrorResponse"
ok: false
status: 400
statusText: "Bad Request"
url: "/api/medical/organisation/1/Participant/filter-by-registration?Filter=Invited&Start=Mon%20Oct%2007%202019%2012:58:46%20GMT%2B0200%20(Central%20European%20Summer%20Time)"
__proto__: HttpResponseBase
所以我的问题是。如何在客户端填写正确的日期格式。这样就不会抛出错误?
谢谢
答案 0 :(得分:1)
您可以使用JSON.stringify(yourDate)
然后在服务器端使用此值。
否则,在客户端,您可以使用moment(yourDate).format('L');
在您的代码中,它将是:
this.participantService.filterParticipantsByRegistration(1, "Invited", JSON.stringify(this.startDate))
或
this.participantService.filterParticipantsByRegistration(1, "Invited", moment(yourDate).format('L'))