How to compare firebase timestamps?
Asked Answered
S

5

10

I have cloud function code code like this:

console.log(`ts: ${(element.get('expire') as admin.firestore.Timestamp).toDate().toUTCString()} now: ${admin.firestore.Timestamp.now().toDate().toUTCString()}`)
const greater = (element.get('expire') as admin.firestore.Timestamp) > admin.firestore.Timestamp.now()
const lower = (element.get('expire') as admin.firestore.Timestamp) < admin.firestore.Timestamp.now()
console.log(`greater: ${greater} lower: ${lower}`)

In console:

ts: Mon, 08 Apr 2019 20:59:59 GMT now: Fri, 08 Mar 2019 20:19:18 GMT

greater: false lower: false

So how correctly compare to Timestamps?

Seicento answered 8/3, 2019 at 20:32 Comment(0)
P
8

You can do this by comparing the seconds and nanoseconds properties on the Timestamp objects. Or, to make it simpler, and you don't need nanosecond precision, you can just compare the results of the results of their toMillis() values.

Parathyroid answered 9/3, 2019 at 0:21 Comment(3)
Thanks. This is the only solution to compare milliseconds.Seicento
is it possible to create an index for firebase timestamps?Abijah
New URL: cloud.google.com/nodejs/docs/reference/firestore/latest/…Doersten
R
20

As of SDK Version 7.10.0, you can directly compare Timestamp objects with the JavaScript arithmetic inequality comparison operators (<, <=, >, and >=).

To check if two Timestamp objects are temporally identical you must use the isEqual property (docs). Comparing with == or === will not check for temporal equality- returning false if comparing different object instances.

SDK release notes here.

Code snippet pulled from corresponding GitHub issue:

a = new Timestamp(1, 1)
b = new Timestamp(2, 2)
console.log(a < b) // true
console.log(b < a) // false
Rascal answered 19/6, 2020 at 5:10 Comment(0)
P
8

You can do this by comparing the seconds and nanoseconds properties on the Timestamp objects. Or, to make it simpler, and you don't need nanosecond precision, you can just compare the results of the results of their toMillis() values.

Parathyroid answered 9/3, 2019 at 0:21 Comment(3)
Thanks. This is the only solution to compare milliseconds.Seicento
is it possible to create an index for firebase timestamps?Abijah
New URL: cloud.google.com/nodejs/docs/reference/firestore/latest/…Doersten
M
1

According to the documentation, we can use the method valueOf() to compare Timestamp. Here the reference of valueOf

Monograph answered 12/8, 2022 at 20:24 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewFierce
W
1

I can find no documentation for it other than the code itself, but it is worth noting that Timestamp appears to have a _compareTo instance method (at least in my current running version v8.10.1) which returns the sort-friendly -1, 1, and 0 comparison values.

const a = Timestamp.now();

// Some time later
const b = Timestamp.now();

console.log(b._compareTo(a)); // 1
console.log(a._compareTo(b)); // -1
Whitmer answered 8/11, 2022 at 18:2 Comment(0)
F
-1

Have you tried turning your Timestamps into a Date object to be able to then just compare Dates instead of Timestamps?

For example:

const greater = new Date(element.get('expire') as admin.firestore.Timestamp) > new Date();
const lower = new Date(element.get('expire') as admin.firestore.Timestamp) < new Date();
Foretell answered 9/3, 2019 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.