Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Firebase query to compare dates which is a string to the actual date

I mistakenly set a date field as a string when creating the database which i didn't realize and now the documents in the db has really increased so it stores the date number as a string in the firestore database and i want to make a query where i compare if the date is between the start and end date i provided. can anyone help me out?

this is how i stored it in the docmument

date: Date.now().toString()

and the variables for the start date and end date

  startDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
  endDate = new Date(
  new Date().getFullYear(),
  new Date().getMonth() + 1,
   0 ).setHours(23, 59, 59, 999);

and this is the query

return this.firestore
  .collection('orders', (orders) =>
    orders
      .where('date', '>=', this.startDate)
      .where('date', '<=', this.endDate)
      .orderBy('date', 'desc')
  )

this doesn't work, is there a way i can get around this? or i have to restructure the db again?

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Storing a date you want to query on like this is a bad idea and a common anti-pattern:

date: Date.now().toString()

The format that you get from calling toString() ("Wed Oct 05 2011 07:48:00 GMT-0700 (Mountain Standard Time)") is meant for displaying to humans and not suitable for ordering and filtering by code.

You should either store your values as Firestore Timestamp, or as a string format that can be ordered and filtered.


Storing dates as a Timestamp can be accomplished with:

date: Date.now()

With this Firestore will store the value as a Timestamp, which you can then order and filter on with ease by passing either Timestamp or Date objects to the where clauses of your query.


Storing dates as a lexicographically ordered string can be accomplished with:

date: Date.now().toISOString()

With this you will be storing strings in a format like "2011-10-05T14:48:00.000Z", which can be sorted and be used to perform range queries.


Update: As you pointed out, your Date.now().toString() gives you a numeric timestamp in string format, e.g. "1657889475842".

In that case the problem is that you're passing Date objects in the query, and comparing a date to a string will never give a match. You should either pass numbers-as-strings to the where too, or take the approach I recommended above.

about 4 years ago · Santiago Gelvez Report

0

I fixed it. This is the solution

return this.firestore
  .collection('orders', (orders) =>
    orders
      .where('date', '>=', this.startDate.getTime().toString())
      .where('date', '<=', this.endDate.toString())
  )
  .valueChanges({ idField: 'Id' });
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!