I'm trying to use Angular2 date pipe to format a date. I have this class as model for my object:
export class Match {
Id: number;
MatchDate: Date;
constructor(
id: number,
matchDate: Date
) {
this.Id = id;
this.MatchDate = matchDate;
}
}
And I fetch Match objects to my Component:
import {Component, OnInit} from 'angular2/core';
import {Match} from '../core/models/match';
import {MatchService} from '../core/services/match.service';
@Component({
selector: 'home',
templateUrl: './app/components/home.html',
directives: [],
providers:[MatchService]
})
export class Home implements OnInit {
nextMatch: Match;
lastMatch: Match;
errorMessage: string;
constructor(
private _matchService: MatchService
) { }
getNextMatch() {
this._matchService.getNextMatch()
.subscribe(
nextMatch => this.nextMatch = nextMatch,
error => this.errorMessage = <any>error
);
}
getLastMatch() {
this._matchService.getLastMatch()
.subscribe(
lastMatch => this.lastMatch = lastMatch,
error => this.errorMessage = <any>error
);
}
ngOnInit() {
this.getNextMatch();
this.getLastMatch();
}
}
In my template I try to display the MatchDate:
<div class="row">
<div class="col-md-6 col-xs-12">
<div *ngIf="lastMatch" class="card">
<div class="card__content">
<p>{{lastMatch.MatchDate | date:"short"}}</p>
</div>
</div>
</div>
<div class="col-md-6 col-xs-12">
<div *ngIf="nextMatch" class="card">
<div class="card__content">
<p>{{nextMatch.MatchDate}}</p>
</div>
</div>
</div>
</div>
When using {{nextMatch.MatchDate}}
I get the correct date, but not formatted as I want. If I use {{lastMatch.MatchDate | date:"short"}}
I get an error saying:
Invalid argument '2016-02-01T18:30:00' for pipe 'DatePipe'
Shouldn't MatchDate be a Date object, not a string?
EDIT:
Here's part of my MatchService:
getNextMatch() {
return this.http.get(this._apiUrl + '/next')
.map(res => <Match>res.json())
.catch(this.handleError);
}