2

I'm trying to query Firestore documents by a Timestamp field (e.g. between a start and end date). Timestamp field returns something like Timestamp(seconds=1599170400, nanoseconds=0).

I have a Unix timestamp (in seconds) in my app which I like to compare this to (using isLessThan:), but the value in Firestore apparently is inside this Timestamp object.

How would I go about comparing these two timestamps?

An alternative would be to query all documents and compare afterwards, but I would like to filter the documents in the query itself.

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • Does this answer your question? [Firestore query by date range](https://stackoverflow.com/questions/47000854/firestore-query-by-date-range) – rdiaz82 Sep 09 '20 at 05:26
  • Incase anyone is struggling here is the solution https://stackoverflow.com/questions/54014679/return-type-of-timestamp-from-firestore-and-comparing-to-datetime-now-in-flutt – Luqman Tuke Sep 06 '21 at 14:38

1 Answers1

6

To compare a Timestamp in the database with a milliseconds-since-the-epoch value, you can create a Timestamp object based on that value:

var timestamp = Timestamp.fromMillisecondsSinceEpoch(1599573193925);

Then you can use this in the query against the value in the database:

Firestore.instance
.collection('items')
.orderBy('Timestamp', descending: true).startAt([timestamp])

Or (my usual method):

Firestore.instance
.collection('items')
.where("Timestamp", isGreaterThanOrEqualTo: timestamp)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807