After exploring numerous threads related to this issue and spending several days trying to find a solution, I may have stumbled upon a potential fix. However, the workaround feels too messy for my liking.
Similar to other users, I am encountering an issue with the Angular Material Datepicker, specifically mat-datepicker
.
When I log the date value, it displays correctly as:
Wed Dec 24 1999 00:00:00 GMT+0100 (Mitteleuropäische Normalzeit)
But in the request payload, it appears as:
1999-12-23T23:00:00.000Z
My attempts so far include adding
{ provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS, useValue: { useUtc: true } }
to both my component
and app.module.ts
, but this has not resolved the issue.
As a temporary fix (before sending the request), I have implemented the following workaround:
let newDate = new Date(this.personalForm.get("dateOfBirth").value);
newDate.setMinutes(newDate.getMinutes() - newDate.getTimezoneOffset());
After making this adjustment, the console now logs:
Wed Dec 24 1999 01:00:00 GMT+0100 (Mitteleuropäische Normalzeit)
And the request payload is now correct:
1997-12-24T00:00:00.000Z
However, this solution may not work for individuals in different time zones such as GMT-0100. How can this issue be rectified properly?
It is worth noting that I also dynamically change the adapter if needed, as shown below:
this._adapter.setLocale(this.translateService.currentLang);