Home > other >  How to use query from firestore using timestamp
How to use query from firestore using timestamp

Time:05-30

I am using a date range picker where I will let my user set the date range of the data that they will query from firestore. I already can get the value of the date range picker and store it from a variable. My problem is I don't know how to query on firestore referencing timestamps.

this is my function for querying documents from a firestore collection. So far I can only collect all data but can't collect filtered data (by timestamps).

export async function Getfs(){
console.log("Get firestore run >>");
var f = document.getElementById("firstDate").value; // starting date
var s = document.getElementById("secondDate").value; // ending date
const fs_snap = await getDocs(query(collection(fs, "sens02"),orderBy('ts')));
}

This is what my firestore document looks like

and this is how I designed my date range picker using html input date date range picker

CodePudding user response:

You need to use the methods of the Timestamp class.

More precisely, in your case, you need to use the fromDate() as shown in the below example:

const q = query(
  collection(fs, "sens02"),
  where('ts', '>=', Timestamp.fromDate(new Date('2022-05-28'))) // For example we use the 28/05/2002
);

const querySnapshot = await getDocs(q);

querySnapshot.forEach((doc) => {
  console.log(doc.data().ts.toDate());
});
  • Related