awesomemypro
awesomemypro

Reputation: 561

How to populate date input in Angular reactive forms?

I have a FormGroup that takes date from a date input.

FormControl:

dob: new FormControl(new Date()),

Assigning new Date() to get the default date, in case no date is selected.

Template:

<input type="date" formControlName="dob">

However, when I am using patchValue to populate the date (from a previously persisted object), it is not workning/showing up in the date input tag's text (in the webpage). What am I missing?

Upvotes: 0

Views: 4692

Answers (1)

Zunayed Shahriar
Zunayed Shahriar

Reputation: 2723

You should format the date using formatDate function form @angular/common namespace before the patch or initialize.

E.g: dob: new FormControl(formatDate(new Date(), "yyyy-MM-dd", "en"))

Working demo at StackBlitz.

Upvotes: 3

Related Questions