It is possible to change date format by button? For example, the default format is DD/MM/YYYY, but when i'm click the button i would register new format, for example YYYY/MM/DD it is possible ? Im started with that code:
part of ts file
const moment = _rollupMoment || _moment;
export const MY_FORMATS = {
parse: {
dateInput: 'DD/MM/YYYY',
},
display: {
dateInput: 'DD/MM/YYYY',
monthYearLabel: 'MM YYYY',
dateA11yLabel: 'DD/MM/YYYY',
monthYearA11yLabel: 'MM YYYY',
},
};
providers: [{
provide: DateAdapter,
useClass: MomentDateAdapter,
deps: [MAT_DATE_LOCALE]
},
{
provide: MAT_DATE_FORMATS,
useValue: MY_FORMATS
},
]
changeFormat() {
????????
}
html file:
<input matInput [matDatepicker]="picker" placeholder="mm.dd.yyyy"
(click)="picker.open()" #birthInput>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
<span (click)="changeFormat()"></span>
Thanks for any help.
You're defining a fixed MY_FORMAT, but you can define a class like
Then in provider you use this class
Finally, you inject in contructor of your component the provider to access to the variable "value"
Well, you need, when change the format "redraw" the dateForm, e.g.
But, be carefully, you need use a CustomDateAdapter or use the MomentDateAdapter
Using a MomentDateAdapter it's import in the module
MomentDateModuleIf you want to use a CustomDateAdapter you can use some like
And you inject in constructor the adapter adn cahnge the "formato" value
You can see a stackblitz using MomentAdapter
You can see a stackblitz using CustomDateAdapter