MongoDB date query

I have a collection named practices that stores documents of practices done by the users. I have 4 keys in each document.

  • startTime (date timestamp)
  • endTime (date timestamp)
  • user (objectID of user)
  • duration (length of practice in secs)

Now here is the confusion. As my timezone is (+5:00 from UTC), suppose I submit a practice with startTime=2024-10-10T01:00:00+05:00 which will be stored in MongoDB as startTime=2024-10-09T20:00:00Z as MongoDB stores date-time in UTC which will move back submitted timestamp to -5 hours, eventually moving to previous date from 10 to 09. Different users will have different timezones.

Now, I want to query a user’s whole practices for the day. I use dayjs for date manipulation.

I generate query like this:

const queryDay = '2024-10-10'

query = {
  startTime: {
  $gte: dayjs.utc(queryDay).startOf('day).toDate()
 },
  endTime: {
  $lte: dayjs.utc(queryDay).endOf('day).toDate()
 }
}

which results startTime=2024-10-10T00:00:00Z and endTime=2024-10-10T23:59:59Z.

Now, when the query runs it will miss the above submitted practice as it was saved with date-time that does not fall in the query but technically it should be included as it was happened on 10 but according to (+5:00).

According to my thinking start day and end day will different according to timezone. When my day starts which is at 2024-10-10T00:00:00+05:00, this time when converted to UTC equals to 2024-10-09T19:00:00Z and same for the end of day, day ends at 2024-10-10T23:59:59+05:00 which in UTC equals to 2024-10-10T18:59:59Z. if we use these startTime and endTime then it will accurately fetches all practices of the day.

These are my main questions:

  1. So how should I do it? Meaning how I build a query respecting user
    timezone who is fetching it.
  2. I have a doubt in toDate() method of dayjs. It behaves differently in node and browser environment. In browser it gives me a date object which is parsed in local timezone. In my case (+5:00) and when in node, it simply first converts the date in UTC then in date object (does not parses in server timezone or any). What would happen if in node it converts the date object into local timezone where the server is running because it will mess up the query?
  3. Is there a way that we can use a ISO string to use it in query. As I heard that it only uses date object to query date fields and does not accept string (ISO date string)? I don’t want to use toDate() as it’s behavior is not consistent.