I'M making an angular2 application and right now i'm trying to parse a string into a object. Basically what is happening is I'm passing back a string, and i want to convert it into a unique object called IDate.
I get a string in this format "2017-02-27T00:00:00", but i want it as IDate(). So what i have done so far is created a IDate class, that takes a string and converts it to a date. I created a Mock class to map the response to an object. However, when i print the object, i see the value stays a string. Anyone know how to achieve what I'm talking about? I'm basically trying to map the string to an object when i map the response from the controller. I could do this in an operation like, resposne.map(r.time => new Date(r.time));
But i rather have it mapped in the response, because i call this controllers in other places and i dont want to repeat the process on separate pages. Can this be done?
Controller
return Json(new { Number = 1, Time= "2017-02-27T00:00:00" });
Client
class IDate {
value: Date;
contructor(value) {
this.value = new Date(value);
}
}
class Mock {
number: number;
time: IDate;
}
mock: Mock = null;
this.service.get()
.subscribe(
response => {
console.log("Test response");
this.mock = response;
console.log(this.mock);
},
Console
Object
number:1
time:"2017-02-27T00:00:00"
__proto__:Object