0

I am using Firestore to store the details of users. I have a field datetime which is a server timestamp, which denotes the time of user creation. I need to get the users created today. Is there any direct way so that I need not to worry about the wrong date/time set in the android device of the user. How should I query users created between a timestamp?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jaspal
  • 601
  • 2
  • 13
  • 23

1 Answers1

4

You should query your Cloud Firestore database using a query that should look somethig like this:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference usersRef = rootRef.collection("users");
Query query = usersRef
    .whereGreaterThanOrEqualTo("timestamp", startingTime)
    .whereLessThanOrEqualTo("timestamp", endingTime);

In Cloud Firestore is allowed to chain multiple where clauses in a single query.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • thanks for reply, but the real problem here is that i want the users created **today** , and how to get that today???? – Jaspal Jan 30 '18 at 12:45
  • When you are using the server timestamp you need to know that is a locale-agnostic and does not require formatting and parsing agreements between clients. So it's better to use this rather than trusting the clients to have their clocks set correctly. So if you want to know users "today", you need to check this client side, not server side. – Alex Mamo Jan 30 '18 at 12:55
  • [This](https://stackoverflow.com/questions/2517709/comparing-two-java-util-dates-to-see-if-they-are-in-the-same-day) is how you can compare two dates on user side. – Alex Mamo Jan 30 '18 at 13:41
  • Thanks #alex. Sure please. – Jaspal Feb 18 '18 at 11:58