如何比较firebase时间戳?

问题描述 投票:1回答:2

我有这样的云功能代码:

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}`)

在控制台中:

ts:星期一,08年4月8日20:59:59格林威治标准时间:星期五,2019年3月8日20:19:18 GMT

更大:false更低:false

那么如何正确地比较Timestamps?

javascript firebase google-cloud-firestore firebase-admin
2个回答
1
投票

您可以通过比较Timestamp对象上的secondsnanoseconds属性来完成此操作。或者,为了使其更简单,并且您不需要纳秒精度,您可以只比较其toMillis()值的结果。


0
投票

您是否尝试将时间戳转换为Date对象,然后才能比较Dates而不是Timestamps?

例如:

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();
© www.soinside.com 2019 - 2024. All rights reserved.