So I am trying to display this date for an input. But it\'s not displaying when setting the value of it in my form builder group. I would also like to be able to format it too.<
You have two possibilities to set you date as default value.
Variant 1:
For setting default date value to tag with type date you should use HTML-property
value
of input-tag.
Example:
Variant 2 (recommended):
You can use an built-in function formatDate()
in angular.
Step 1:
Import formatDate()
from @angular/common
in you component typescript file.
import { formatDate } from '@angular/common';
Note: formatDate(yourDate, format, locale)
expects 3-4 parameters.
Step 2:
Format your dates in definition of your form group.
this.editForm = this.formBuilder.group({
startDate: [formatDate(this.post.startDate, 'yyyy-MM-dd', 'en'), [Validators.required]],
endDate: [formatDate(this.post.endDate, 'yyyy-MM-dd', 'en'), [Validators.required]]
});
Here is working example: https://stackblitz.com/edit/angular-kucypd
Here is documentation of input type date: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date