I want to output from the JSON the date in the input field of the mat-datepicker. When I run the output I always get the current year. I am working with ReactiveForms. What am I doing wrong?
My code:
// JSON
{
"success": true,
"mandant": {
"mandantId": 2,
"firm": "Test GmbH",
"street": "Test-Street",
"number": "2",
"zip": "5xxxx",
"city": "Exxxxxxxx",
"country": "Germany",
"financial_year_start": "Jan. 1, 2020" // This is to be output in the input
}
}
// TS
public getFiscalYearStart: string;
ngOnInit() {
// To initialize forms
this.initForm();
// To initialize loadFinancialYearStart
this.loadFinancialYearStart();
}
// Creation of the settingsContentForm
private initForm() {
// General
this.settingsContentForm = this.formBuilder.group({
financial_year_start: new FormControl(moment(this.getFiscalYearStart), Validators.required),
});
}
/**
* Fill the datePicker with data
*/
loadFinancialYearStart() {
this.userAreaService.getCurrentMandantData().subscribe(resp => {
this.getFiscalYearStart = resp.mandant.financial_year_start;
console.log(this.getFiscalYearStart); // Correct year is displayed in the console
});
}
This might be related to Date format issue and DatePicker is showing current date because it's not able process the date string.
Give it a try and make below changes in JSON