I would like to expose my date value as date only ("yyyy-MM-dd") on an Angular Material date picker. Currently, the default date value is an ISO format (includes time).
I have a starter Stackblitz project with the the required setup. The formly-datepicker.type
is where I would like to centralize the conversion so that any date used in the model will be formatted properly for the service layer.
One of my attempts to accomplish it in a centralized manner is using the Angular ControlValueAccessor, but the value is never updated. ControlValueAccessor Stackblitz
In the example Stackblitz, after changing the date and clicking Submit the value I need returned would be something like: {"mydate":"1940-03-11"}
(without the time information).
So , I think I have solution for you to display
mat datepicker
as("yyyy-MM-dd")
format. To do that Install to dependencies"@angular/material-moment-adapter": "^11.1.2"
and"moment": "^2.18.1"
(both are current version). Then import in yourformly-datepicker-type.component.ts
file these 2 libraries.Then add a date format like below in your
formly-datepicker-type.component.ts
file.=>After that Update
@Component
by adding providers like below =>Finally, Date time will show as wanted.
Note: I have updated you sample code of stackblitz. Please check my demo link =>Stackblitz mat-datepicker Date-Time Format.