Home > Software engineering >  Convert Datepicker Date to ToIsoString
Convert Datepicker Date to ToIsoString

Time:11-08

i want to convert date from datepicker to toisostring in angular. my problem is when i add data from to backend a have date day before one day my code is HTML:

   <mat-form-field appearance="fill">
      <mat-label>Enter a date range</mat-label>
      <mat-date-range-input  [rangePicker]="picker" separator="to" required [min]="today" [dateFilter]="dateFilterFn">
        <input matStartDate formControlName="From_Date" placeholder="From_Date" name="From_Date">
        <input matEndDate formControlName="To_Date" placeholder="To_Date" name="To_Date">
      </mat-date-range-input>
       <mat-hint>DD/MM/YYYY – DD/MM/YYYY </mat-hint>
      <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
      <mat-date-range-picker #picker></mat-date-range-picker>
    </mat-form-field>

File Ts:

this.demandeForm = this.formBuilder.group({
    From_Date:[],
     To_Date :[],
      Created_AT : [this.created_AT,Validators.required],
      nmbJours : ['',Validators.required],
      alternate : [''],
      details:[''],
      status:['En Cours'],
      reason:[''],
      employeeId : ['',Validators.required],
      typecongesId : ['',Validators.required]

  })

when i insert data i have this in console From_Date:Mon Nov 07 2022 00:00:00 GMT 0100 (UTC 01:00) {} To_Date:Fri Nov 11 2022 00:00:00 GMT 0100 (UTC 01:00) but in backend i have : From_Date: 06-11-2022
TO_Date : 10-11-2022

CodePudding user response:

You can try this

const event = new Date("Fri Nov 11 2022 00:00:00 GMT 0100 (UTC 01:00)");

here is result

"11/11/2022"

Best Regards!

CodePudding user response:

I have solved it:

this.demandeForm.patchValue({From_Date: new Date(koko.getTime() - koko.getTimezoneOffset() * 60000).toISOString() });

  • Related