skip to Main Content

I have a calendar that returns a date in "Date" format, so I’m trying to compare a date obtained by the user with a date stored in firestore as Timestamp, but I can’t get it because of the way firestore returns its timestamp.

Is there any way to do something like this? :

async function getBaristaShift(date: Date, db: Firestore) {
 date.setHours(0, 0, 0, 0);
 const shiftsRef = collection(db, "shifts");
 const queryStatement = query(shiftsRef, where("date", "==", date)
}
getBaristaShift(new Date(now), dbRef);

This will not find any docs as the date formats are not the same even tho the dates are the same

2

Answers


  1. A timestamp in Firestore identifies a very precise moment in time, up to the nano-second. If you want to query that timestamp with the == operator, you will have to have the exact same value in your code as you’ve stored in the database.

    While that is possible, it is much more common to query the timestamp field with relational operations such as >, >=, < and <=. For example, to find all document with the same date, you could do:

    var tomorrow = Date();
    tomorrow.setDate(date.getDate() + 1);
    

    And then:

    query(
      shiftsRef, 
      where("date", ">=", date),
      where("date", "<", tomorrow)
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search