Reputation: 3987
I'm making an angular2 application and all the dates i get back from my controller are in this format "2017-02-2700:00:00". I would like to extend the js Date object so that i remove the T from the string when i make a new object and it still acts as a date object. The current way I'm trying to achieve this is not working. The IDate object i made that extends the Date object is just showing up as a null object instead of an date object. Anyone know whats going on and how to fix it. I'm willing to use es6 js instead of typescript if it cant be done in typescript. I saw the post about extending the Date object, but that modifies the date object, i was hoping to have my own object called IDate that was the same as Date but with a few more methods. Ill mark that the answer if that is the only way this can be done.
export class IDate extends Date {
constructor() {
super();
}
value;
toDate(value: string): Date {
value.replace("T", " ");
return new Date(value);
}
getvalue() {
return this.value;
}
}
let t = new IDate();
let d = new Date();
console.log(t);
console.log(d);
console
IDate {}
Fri Mar 31 2017 11:53:59 GMT-0500 (Central Daylight Time)
Upvotes: 3
Views: 3466
Reputation: 164307
You can only extend native (built-in) types in this way if you target es6
.
If you can't target es6
then you need to workaround it: Extending built-ins like Error, Array, and Map may no longer work.
More info on this:
While not all of these talk about extending Date
, the problem is still exactly the same.
Upvotes: 3