mightycode Newton
mightycode Newton

Reputation: 3949

How to show the correct date in format of datepicker in Angular material

I have a Angular 8 application. And I am using the Datepicker of material.

But if I fill in a date an call the get api call:

  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);
      })
    );

And the template looks like this:

    <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>

So on the server side the date will be show as: jjjj-MM-dd.

But on the client side the date has to been shown as: dd-MM-jjjj.

But so if I call the api call:

console.log(
      this.participantService.filterParticipantsByRegistration(1, "Invited", this.startDate.toString()).subscribe(result => {
        console.log(this.startDate.toString());
        console.log(result);
      })
    );

Then I will get this error:

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

So my question is. how to fill in the correct date format on the client side. So that it will not thrown an error?

Thank you

Upvotes: 0

Views: 715

Answers (1)

NikNik
NikNik

Reputation: 2301

You can use the iso date format using JSON.stringify(yourDate)

then on server side just use this value.

Otherwise, on client side, you can use moment(yourDate).format('L');

https://momentjs.com/

In your code, it would be:

this.participantService.filterParticipantsByRegistration(1, "Invited", JSON.stringify(this.startDate))

or

this.participantService.filterParticipantsByRegistration(1, "Invited", moment(yourDate).format('L'))

Upvotes: 1

Related Questions