I am working on an Angular project using FireStore database and I have the following problem:
Into my Firestore database a I have documents like this:
As you can see in the previous pic the start and end fields are defined on Firestore as TimeStamp fields.
Into a service class I retrieve all these documents in this collection and I return these documents as a list of Observable by this method:
getEvents(): Observable<any[]> {
this.items = this.db.collection('calendar').valueChanges();
return this.items;
}
Then into the ngOnInit() method of my component class I do:
ngOnInit() {
this.eventService.getEvents().subscribe(events => { this.events = events.map((event) => {
console.log("START: ", event.start);
//var date = new Date(event.start);
var date = new Date(0); // The 0 there is the key, which sets the date to the epoch
date.setUTCSeconds(event.start);
var hour = date.getHours();
console.log("START DATE: ", date);
console.log("START HOUR: ", hour);
// ...
}
and here my problem: the value of the event.start is not a date but an object like this (this is the console.log() output):
START: t {seconds: 1487257200, nanoseconds: 0}
From what I know this object represents my date but how can I convert it again into a Date object.
As you can see I tried doing in this way:
var date = new Date(0); // The 0 there is the key, which sets the date to the epoch
date.setUTCSeconds(event.start);
but doing in this way I am obtaining a wrong date, in fact I am obtaining:
START DATE: Sun Feb 16 3986 07:00:00 GMT-0800 (Ora standard del Pacifico USA)
the month and the day (Feb 16) are correct but the year is totally wrong (3986 instead 2017).
What is wrong? What am I missing? What is the correct way to obtain a Date object from the retrieved Firestore Timestamp?