I am using angular material date picker but when I am sending date to my backend. It is sending date which is 1 day before. So if I select 6th February then while sending to the backend it selects 5th Feb. My date value shown in json console as "treatmentDate":"2021-02-05T18:30:00.000Z"
code for Datepicker
<mat-form-field appearance="outline">
<mat-label>Choose Treatment Date</mat-label>
<input matInput [matDatepicker]="dp3" formControlName="treatmentDate" required>
<mat-datepicker-toggle matSuffix [for]="dp3"></mat-datepicker-toggle>
<mat-datepicker #dp3 disabled="false"></mat-datepicker>
</mat-form-field>
Post Model
let postPatientInformation: PostTreatmentInformationModel = {
id: treatmentInformationFormData.id,
title : treatmentInformationFormData.treatmentTitle,
summary: treatmentInformationFormData.treatmentSummary,
patientId: this.treatmentInformation.patientId,
treatmentDate: this.formatDate(treatmentInformationFormData.treatmentDate),
treatmentFiles: this.treatmentFiles == null ? null : this.treatmentFiles
};
console.log("In post treatment", JSON.stringify(postPatientInformation));
Issue solved by formatting the date to remove offset
var offsetMs = treatmentDate.getTimezoneOffset() * 60000;
return new Date(treatmentDate.getTime() - offsetMs);
How to fix this issue? Also, after researching I found out that this issue is related to formatting and many suggest to use moment with angular material datepicker. But I do not know how to use it.
question from:
https://stackoverflow.com/questions/66053007/angular-material-date-picker-sending-date-1-day-before-and-how-to-use-moment-wit 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…