Angular 7:如何正确解析后端响应以从字符串中提取日期?

Angular 7: How to properly parse backend responses to extract Date from string?

我有返回电影服务的功能(还有另一种返回电影集合的方法):

postNewMovie(movie: Movie): Observable<Movie> {
    const requestUrl = `${apiUrl}/create`;
    const movieJSON = JSON.stringify(movie);
    return this.http.post(requestUrl, movieJSON, httpOptions).pipe(
        tap((result: Movie) => console.log(`Posted movie with id = ${result.id} and title = ${result.title}!`))
    );

模型看起来像这样:

export class Movie {
    (...)
    releaseDate?: Date;

    constructor(obj: any) {
      (...)
      this.releaseDate = obj.releaseDate;
    }
}

现在,如何从后端 api 调用(采用 ISO8601 格式)正确转换字符串发布日期,我应该在哪里做?我看到类似的答案,建议在 JSON 解析器中使用一些自定义的 reviver 函数,但我也可以在模型的构造函数中这样做,那么最好的方法是什么?

你认为我可以使用 moment.js 库进行此类转换还是 "overkill"?

在你的构造函数中尝试它应该工作的模型

 this.releaseDate = (obj.releaseDate ) ? new Date(obj.releaseDate ) : new Date();