我有这样的云功能代码代码:
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:2019 年 4 月 8 日星期一 20:59:59 GMT 现在:2019 年 3 月 8 日星期五 20:19:18 GMT
更大:假 更低:假
那么如何正确地与时间戳进行比较呢?
原文由 user1717140 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以通过比较 Timestamp 对象上的 秒 和 纳秒 属性来做到这一点。或者,为了更简单,不需要纳秒精度,你可以只比较它们的 toMillis() 值的结果。