Firestore query on timestamp field

755 views
Skip to first unread message

Adam

unread,
Nov 27, 2018, 5:55:45 PM11/27/18
to Firebase Google Group
I'm relatively new with firestore and wasn't able to find a solution to my problem online so I thought I might post it.

I'm looking to query documents in an existing collection.  The documents all have a timestamp field.

When I attempt to query for all documents ">" or "<" right now, the query works fine.  When I attempt to query for ">" or "<" 7 days ago, the query returns nothing.  I'm sure that I'm probably just missing something small.  Thanks for any help!

These return documents as expected:
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
and
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '<', today).get().then(function(querySnapshot) {

These don't return anything:
var today = new Date()-604800000;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {
and
var today = new Date();
db.collection("****").where('display', '==', true).where('createdAt', '>', today-604800000).get().then(function(querySnapshot) {
and just for the heck of it
var today = new Date()-1;
db.collection("****").where('display', '==', true).where('createdAt', '>', today).get().then(function(querySnapshot) {



If someone could point out what I'm doing wrong, I would greatly appreciate it.  Thanks!

Kato Richardson

unread,
Nov 28, 2018, 1:12:24 PM11/28/18
to fireba...@googlegroups.com
Hi Adamo,

Try this:
const milliseconds = Date.now() - 604800000;
const dateObject = new Date( milliseconds );

....where('createdAt', >, dateObject)...

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/1aa9ee68-c6dc-4de0-8e58-8002a53b4b0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Adam

unread,
Nov 28, 2018, 11:37:04 PM11/28/18
to Firebase Google Group
This worked exactly as desired.  Thanks for the assistance!
Reply all
Reply to author
Forward
0 new messages