2

Pretty basic question in here

My Stored Date String is like this in Firebase DB

23-Oct-2016 07:00:11 AM

Now i am firing this query , i am getting all the data starting with that date

   Query query = mDatabase.orderByChild("time").startAt("23-Oct-2016")

Now I also want to filter by current AM/PM time i am trying query like this but it has no effect on results.

Basically it should not return any object if the current time is PM , i don't know why its not working .

   Query query = mDatabase.orderByChild("time").startAt("23-Oct-2016").endAt("PM");
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Heisenberg
  • 313
  • 1
  • 2
  • 17

1 Answers1

4

Storing dates and times as readable strings like you seem to be doing is guaranteed to give you a hard time. Strings are lexicographically ordered, while you typically want to treat dates as chronologically ordered. The two orders don't always match up.

The best way to store a date/time for querying is as a simple timestamp: the number of milliseconds since the UNIX epoch.

If you insist on storing the dates and times as more readable string, your better of storing them in a format that is both readable and will sort the same in both lexicographical and chronological order, such as the ISO 8601 formats: 20161023T150422. This is the current time as I type this: October 23, 2016 3:04 PM UTC.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • What I did wrong is that I considered startAt() and endAt() as like string prefix and string end compare stuff. I am new to firebase, I understood now that it's range. So basically passing only PM is not going to give me anything as it's not proper range. I need to filter data every 12 hours. I hope you got my point. – Heisenberg Oct 23 '16 at 17:43
  • The structure I describe above allows you to filter the data based on many date/time ranges. – Frank van Puffelen Oct 23 '16 at 17:47