Aymen Kanzari
Aymen Kanzari

Reputation: 2013

Angular 6 - Unable to convert DatePipe

I want to set a current date in my formGroup with specific format, i tried with the below code, but i get this error

'Unable to convert "13/07/2020" into a date' for pipe 'DatePipe'

this.fb.group({
      startdateActivity: [this.datePipe.transform(new Date().toLocaleString().substring(0, 10), "yyyy-dd-MM"), Validators.required]
})

<input type="date" formControlName="startdateActivity"/>

Upvotes: 0

Views: 633

Answers (1)

surendra kumar
surendra kumar

Reputation: 1780

Use the code below.

If startdateActivity type is string

   let date = this.datePipe.transform(new Date(), "yyyy-dd-MM");
    this.fb.group({
          startdateActivity: [date.toString(), Validators.required]
    });

If startdateActivity type is date

 let date = this.datePipe.transform(new Date(), "yyyy-dd-MM");
    this.fb.group({
          startdateActivity: [date, Validators.required]
    })

Upvotes: 1

Related Questions