When working with Angular 5, I have encountered an issue where JSON data from an API is not properly deserialized into dates within an array. My model includes a definition like this:
export interface ProcessDefinition {
_id?: string;
processDefinitionName: string;
myDates: Date[];
}
To fetch data from the API, I use HttpClient in a service class that returns an observable:
public getItems(): Observable<ProcessDefinition[]> {
let url = this.apiUrl + this.entityName + '/';
this.loggerService.log(`Getting items with observable via http.`);
this.loggerService.log(`URL: ` + url);
return this.httpClient.get<ProcessDefinition[]>(url);
}
In my component, I call this service method as follows:
public loadProcessDefinitionData(): void {
this.processDefinitionService.getItems().subscribe(items => {
this.processDefinitions = items;
// Various attempts to convert dates
this.currentProcessDefinition = this.processDefinitions[0] || null;
this.currentProcessDefinition.day1dates.forEach(dat => dat = new Date(dat));
console.log(JSON.stringify(this.currentProcessDefinition.day1dates[0].getMonth()));
});
}
Despite trying different approaches, I have been unable to successfully convert the dates into real dates using the 'new Date' approach suggested in similar discussions. I am seeking assistance to ensure that the dates in the ProcessDefinition[] observable are genuine, allowing me to perform operations like calling getMonth() on them.
If possible, I would like to handle this date conversion logic within the service to keep it consolidated in one place.
Thank you