In an Angular application, I'm getting a JSON from the server which contains a date, the date is like "2022-08-05"
in developers tools -> network -> response.
Using an HTTP request, I'm putting the JSON inside the following interface:
export interface Movie {
id: number
title: string
releaseDate: Date
director: string
description: string
}
When performing movie.releaseDate.getFullYear()
, the following error shows in console: movie_r2.releaseDate.getFullYear is not a function
Then, I tried using a class with a property decorator but that doesn't work either.
Afterwards, I tried with getter and setter, but I get the same error.
// function asDate(target: Object, propertyKey: string) {
// let value: string;
// console.log("asDate() called");
//
// Object.defineProperty(target, propertyKey, {
// get: () => new Date(value),
// set: (newValue) => value = newValue
// });
// }
export class Movie {
id: number
title: string
// @asDate
private _releaseDate: Date
director: string
description: string
constructor(id: number, title: string, releaseDate: Date, director: string, description: string) {
console.log("constructor called");
this.id = id;
this.title = title;
this._releaseDate = new Date(releaseDate);
this.director = director;
this.description = description;
}
get releaseDate(): Date {
console.log("called getter");
return new Date(this._releaseDate);
}
set releaseDate(releaseDate: Date) {
console.log("called setter");
this._releaseDate = new Date(releaseDate);
}
}
Why are the console.log
statements in the descriptor, constructor, getter, setter never called?
How to write some code at the level of the Movie class/interface/type such that, for any HTTP request which contains the type Movie, the field releaseDate
will contain an object of type Date (and not a string)? I have more than one HTTP request with a Movie interface/class, so I'm interested in a solution that is independent of the HTTP request. Meaning that I write it once, and it works at any request.