skip to Main Content

I am creating a Todo app in Angular using Firestore as my backend. I am really new to Firebase and I am having trouble storing and fetching dates.

When I get the dates from firebase and console.log them i get something like this: date: _Timestamp { seconds: 1736290800, nanoseconds: 0 }

I understand that to get a human readable date out of this i need to access the seconds part of this and convert it. I have tried to use date.seconds and date.getSeconds() but .seconds gives me this error:

[ERROR] TS2339: Property ‘seconds’ does not exist on type ‘Date’. [plugin angular-compiler]
src/app/pages/todolist/todolist.component.ts:79:47:
  79 │         console.log("date: ",todo.deadlineDate.seconds);

And date.getSeconds() is not recognized as a function.

It seems like the date retrieved from firebase is not recognized as a Timestamp object.

I have tried making a Timestamp object of the variable using Timestamp.fromDate() but it did not work.

I am using Angular 18 with AngularFirestore and the dates are from user input with MatDatePickerModule and MatNativeDateModule and is stored as a Date in the object sent to firebase.

Appreciate if someone would have a solution to this.

2

Answers


  1. Chosen as BEST ANSWER

    What do you mean with deadlinedate === function? I tried toDate but it does not seem to be an option of that object


  2. According to what you describe you should be receiving some sort of Firestore.Timestamp object. In this case, you should convert the Timestamp object into a Date type. Something like this:

    const deadlineDate = todo.deadlineDate; // Assuming this is the Firestore Timestamp
    if (deadlineDate && typeof deadlineDate.toDate === 'function') {
        const humanReadableDate = deadlineDate.toDate(); // This will do the trick
        console.log("Converted Date:", humanReadableDate);
    } else {
        console.log("Deadline date is not a valid Firestore Timestamp.");
    }
    

    After that, you can use it with the Angular date pipe to show it in the template.

    Source: https://firebase.google.com/docs/reference/js/firestore_.timestamp.md#timestamptodate

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search