简体   繁体   中英

How to use query from firestore using timestamp

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

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());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM