0

i have these two "where" and if I'll add the "where" for the date, it's not working anymore. I just watn to show all of the orders with the order status of confirmed and a delivery date of today.

var start = new Date();
start.setUTCHours(0, 0, 0, 0);
var startOfDay = start.toLocaleDateString();
console.log(startOfDay);

var end = new Date();
end.setHours(23, 59, 59, 999);
var endOfDay = end.toUTCString();
console.log(endOfDay);

try {
  firestore
    .collection("orders")
    .where("orderStatus", "==", "Confirmed")
    .where("deliveryDate", ">=", startOfDay)
    .where("deliveryDate", "<=", endOfDay)
    .onSnapshot((snapshot) => {
      const orders = [];
      snapshot.docs.map((doc) => {
        const data = doc.data();
        orders.push({
          "Order ID": doc.id,
        });
      });
      console.log(orders);
      this.setState({ orders: orders });
      console.log(this.state.orders);
    });
} catch (err) {
  console.log(err);
}

}

This is the timestamp field in firestore enter image description here

enter image description here

JS3
  • 1,623
  • 3
  • 23
  • 52
  • 1
    How could this even work? How could you expect any document to have a date field exactly equal to the date generated on your client? What are you trying to achieve? – l1b3rty May 12 '21 at 09:35
  • I just wanted to get the same date from the one saved in the firestore and the client's date. Like same month,date, and year – JS3 May 12 '21 at 12:09

3 Answers3

0

To get the messages matching a given year, month or day you will have to

  1. Store the year, month and day as separate fields in your document

  2. Change your query (example to query for the day)

    firestore
    .collection("orders")
    .where("orderStatus", "==", "Confirmed")
    .where("day", "==", Math.floor(Date.now()/(1000*3600*24)))
    .get()
    
l1b3rty
  • 3,333
  • 14
  • 32
0

A JavaScript Date object contains more information. According to its docs:

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.

So when you call new Date() is represents that exact moment in time, and it is very unlikely that your database contains a document with that exact same timestamp.

That's why you'll usually want to query on a date range on timestamp fields. So you'll determine the exact dates for the start of the day, and the end of the day, and then do:

firestore
    .collection("orders")
    .where("orderStatus", "==", "Confirmed")
    .where("date", ">=", startOfDay)
    .where("date", "<=", endOfDay)

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I've tried doing it like this and I'm still not getting the desired output. `var start = new Date();' start.setUTCHours(0, 0, 0, 0); var startOfDay = start.toUTCString(); console.log(startOfDay); var end = new Date(); end.setHours(23, 59, 59, 999); var endOfDay = end.toUTCString(); console.log(endOfDay); try { firestore .collection("orders") .where("orderStatus", "==", "Confirmed") .where("deliveryDate", ">=", startOfDay) .where("deliveryDate", "<=", endOfDay)` – JS3 May 14 '21 at 03:34
  • To query on a date, you have to filter on the range of timestamps. If your code doesn't work, update your question to show the new code, and show a document that you'd expect to be returned by the query. – Frank van Puffelen May 14 '21 at 03:42
  • I've edited my question already. Thank you – JS3 May 14 '21 at 03:47
  • You shouldn't convert them to strings. Just pass the `start` and `end` variables (of type `Date`) to Firestore. – Frank van Puffelen May 20 '21 at 01:01
0
 I have already fixed this and this is the updated code by converting the javascript startofDay and endofday to firebase timestamp.

var start = new Date();
    start.setUTCHours(0, 0, 0, 0);
    var startOfDay = start.toLocaleDateString();

    var end = new Date();
    end.setHours(23, 59, 59, 999);
    var endOfDay = end.toUTCString();
  
    var myTimestamp = firebase.firestore.Timestamp.fromDate(
      new Date(startOfDay)
    );

    var ending = firebase.firestore.Timestamp.fromDate(new 
     Date(endOfDay));
    console.log("end", ending);

    try {
      firestore
        .collection("orders")
        .where("orderStatus", "==", "Confirmed")
        .where("deliveryDate", ">=", myTimestamp)
        .where("deliveryDate", "<=", ending)
JS3
  • 1,623
  • 3
  • 23
  • 52