I am trying to format date through a directive using Angular Reactive Forms but with German locale the MatDatepicker seems not to work as wanted.
In my DateDirective I have built the method below:
public ngOnInit(): void {
const control = this.ngControl.control;
if (control) {
control.setValue(Parser.parseDate(control.value, this.locale), { emitEvent: false });
control.valueChanges.subscribe(newDate => {
newDate = Parser.parseDate(newDate, this.locale);
control.setValue(newDate, { emitEvent: false });
});
}
}
If I choose a date from the datepicker, it works fine. For example, I choose on the datepicker the 19th of March, 2021 and on the form field is shown the output of 19.3.2021. But if I write the same value directly in the field, the value of newDate is null. I have noticed that when I write the date in the input field, the datepicker parses it as mm.dd.yyyy. Has anyone any idea about how to get rid of this bug/issue without having to override the NativeDateAdapter?
Thank you!