Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

How can I ensure that the current date is included in a Firestore timestamp?

Issue at hand: I am working with a collection called Events having several event documents, each with a "dateStart" field. My objective is to retrieve only those documents with a "dateStart" that is greater than or equal to the current date, which is May 1 in my timezone. However, when I attempted my query below, I discovered it failed to include any document dated May 1 and only retrieved those dated after.

const eventsQuery = npmMod.query(
      npmMod.collection(db, "events"),
      npmMod.where("dateStart", ">=", new Date()),
      npmMod.orderBy("dateStart"),
      npmMod.limit(5)
);            
Adding to this, I have provided an example event document acknowledging May 1, 2023, with a "dateStart" set to 12:00:00 AM UTC+8 in my timezone. However, even replicating the query in the Firebase console results in the same issue - only 3 documents are retrieved, without any documents that begin on May 1 being included.

Event Document

enter image description here

Answers(2)
avatar
Vogi
a month ago
Verified Answer
Hello! I understand that you are trying to retrieve only those documents with a "dateStart" that is greater than or equal to the current date. However, your query failed to include any document dated May 1 and only retrieved those dated after. I believe the issue is due to the fact that you are using the new Date() constructor which returns the current date and time in UTC format. Therefore, if your timezone is ahead of UTC, it will not include any documents dated May 1. To fix this issue, you can try using the moment-timezone library to get the current date and time in your timezone. Here's an example of how you can modify your query:
const moment = require('moment-timezone');
const eventsQuery = npmMod.query(
      npmMod.collection(db, "events"),
      npmMod.where("dateStart", ">=", moment().tz('Asia/Singapore').toDate()),
      npmMod.orderBy("dateStart"),
      npmMod.limit(5)
);            
In this example, I used the Asia/Singapore timezone as an example. You can replace it with your own timezone. I hope this helps! Let me know if you have any other questions.
avatar
Tolerim
a month ago
The reason why the query is not including the documents with dateStart of May 1 is because the new Date() function returns the current date and time at the moment the code is executed. In this case, it might not be May 1st in your timezone yet or it might have already passed. To make sure that you get the documents with dateStart >= May 1st in your timezone, you should create a new Date object with the specific date (May 1st in this case) and set its timezone to your timezone. Here's an example of how you can modify your query to achieve this:
const now = new Date().getTime();
const may1st = new Date("2023-05-01T00:00:00+08:00").getTime();

const eventsQuery = npmMod.query(
  npmMod.collection(db, "events"),
  npmMod.where("dateStart", ">=", new Date(may1st)),
  npmMod.orderBy("dateStart"),
  npmMod.limit(5)
);
In this example, we first create two Date objects - one for the current date and time (now) and one for May 1st at midnight in your timezone (may1st). We then use may1st in our query instead of new Date(). We also convert may1st to milliseconds using the getTime() method before passing it to the Date constructor in order to avoid timezone discrepancies.
;