Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何比较firebase时间戳?_Javascript_Firebase_Google Cloud Firestore_Firebase Admin - Fatal编程技术网

Javascript 如何比较firebase时间戳?

Javascript 如何比较firebase时间戳?,javascript,firebase,google-cloud-firestore,firebase-admin,Javascript,Firebase,Google Cloud Firestore,Firebase Admin,我有如下云函数代码: 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) > admi

我有如下云函数代码:

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}`)
console.log(`ts:${(element.get('expire')作为admin.firestore.Timestamp.toDate().toutString()}现在:${admin.firestore.Timestamp.now().toDate().toutString()}`)
常量更大=(element.get('expire')为admin.firestore.Timestamp)>admin.firestore.Timestamp.now()
const lower=(element.get('expire')作为admin.firestore.Timestamp)
在控制台中:

ts:2019年4月8日星期一20:59:59 GMT现在:2019年3月8日星期五20:19:18 GMT

较大:false较低:false


那么如何正确比较时间戳呢?

您可以通过比较时间戳对象上的和属性来做到这一点。或者,为了更简单,并且不需要纳秒精度,您可以只比较其值的结果。

您是否尝试过将时间戳转换为日期对象,以便只比较日期而不是时间戳

例如:

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();
const greater=new Date(element.get('expire')作为admin.firestore.Timestamp)>new Date();
const lower=新日期(element.get('expire')作为admin.firestore.Timestamp)
从SDK 7.10.0版开始,您可以直接使用JavaScript算术不等式比较运算符(
=
)比较时间戳对象

要检查两个时间戳对象在时间上是否相同,必须使用
isEqual
属性()。比较
=
==
不会检查时间相等性-如果比较不同的对象实例,则返回
false

已提取代码段:

a=新的时间戳(1,1)
b=新的时间戳(2,2)
console.log(a
谢谢。这是比较毫秒的唯一解决方案。是否可以为firebase时间戳创建索引?
a = new Timestamp(1, 1)
b = new Timestamp(2, 2)
console.log(a < b) // true
console.log(b < a) // false