Currently, I am developing an Angular project that involves using a FireStore database. However, I have encountered a problem with the setup.
Within my Firestore database, I have documents structured like the example shown in this image: https://i.sstatic.net/7z8M6.png
The fields start and end are defined as TimeStamp fields in Firestore, as depicted in the provided screenshot.
In my service class, I fetch all these documents from the collection and return them as a list of Observable using the following method:
getEvents(): Observable<any[]> {
this.items = this.db.collection('calendar').valueChanges();
return this.items;
}
Upon calling the ngOnInit() method in my component class, I perform the following actions:
ngOnInit() {
this.eventService.getEvents().subscribe(events => { this.events = events.map((event) => {
console.log("START: ", event.start);
var date = new Date(0);
date.setUTCSeconds(event.start);
var hour = date.getHours();
console.log("START DATE: ", date);
console.log("START HOUR: ", hour);
// ...
}
Here lies the issue: the value retrieved for event.start is not a standard date format but instead an object represented as follows:
START: t {seconds: 1487257200, nanoseconds: 0}
This object seems to represent the date, but I am unsure how to convert it back into a valid Date object.
I attempted the conversion in the following manner:
var date = new Date(0);
date.setUTCSeconds(event.start);
However, this approach yields an incorrect date:
START DATE: Sun Feb 16 3986 07:00:00 GMT-0800 (Pacific Standard Time)
While the month and day (Feb 16) are accurate, the year is completely off (3986 instead of 2017).
What am I missing? What is the correct method to obtain a valid Date object from the retrieved Firestore Timestamp?