Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

Angular material date picker sending date 1 day before and how to use moment with Angular material datepicker

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I have solution for you. I am using ReavitveForm to solve your problem. Here is my code given below => HTML:

<form [formGroup]="form">

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

</form>

<br><span>Selected Treatment date:</span> {{selectedTreatmentDate}}

TS:

export class DatepickerOverviewExample {
  constructor(
    private fb: FormBuilder) {
      this.form = fb.group({
        treatmentDate:this.treatmentDate
      });
     
       this.treatmentDate.valueChanges.subscribe((v) => {
        this.selectedTreatmentDate = v;
      })
    }
  form: FormGroup;
  treatmentDate= new FormControl(new Date());
  selectedTreatmentDate: Date;
}

NOTE: I have created a stackblitz demo for you. Please Check the link of Stackblitz Demo Code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...