Pooja
Pooja

Reputation: 485

How to query only date (not time) in cloud firestore?

I want to query firestore collection on timestamp field but only on date (NOT TIME). For example I have three records in my database like these:

  1. 7 July 2019 at 06:40:00 UTC+5:30
  2. 9 July 2019 at 03:58:00 UTC+5:30
  3. 11 July 2019 at 07:00:00 UTC+5:30

when I try to query with 07/07/2019 to 09/07/2019, result showing only 1 record, instead of 2. I think its due to time.

I checked this link (Firestore query by dates and times separately) but its not related with my query.

My query in code:

Date dateFrom = "21/07/2019";
Date dateTo = "24/07/2019";
Query MyQuery = mFirestore.collection("students")
.whereGreaterThanOrEqualTo("entered", dateFrom)
.whereLessThanOrEqualTo("entered", dateTo);

I think this is due to different time in database. Please help me to solve this issue.

Upvotes: 1

Views: 1566

Answers (1)

Ban Markovic
Ban Markovic

Reputation: 930

The problem is that when you pass date in query, it automatically append 00:00:00 time. So if you want to query with 07/07/2019 to 09/07/2019, I will advise you to try to add 1 day more to your last date. So in this particular example you should set 07/07/2019 to 10/07/2019. Or maybe another solution is to append on second date 23:59:59 timestamp.

Upvotes: 2

Related Questions