How To Compare Firebase Timestamps?
Solution 1:
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.
Code snippet pulled from corresponding GitHub issue:
a =newTimestamp(1, 1)
b =newTimestamp(2, 2)
console.log(a < b) //true
console.log(b < a) //false
Solution 2:
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.
Solution 3:
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 = newDate(element.get('expire') as admin.firestore.Timestamp) > newDate();
const lower = newDate(element.get('expire') as admin.firestore.Timestamp) < newDate();
Post a Comment for "How To Compare Firebase Timestamps?"