【问题标题】:How to compare firestore timestamps object如何比较 Firestore 时间戳对象
【发布时间】:2019-10-31 05:32:51
【问题描述】:

使用带有 Cloud Scheduler 的 CloudFunctions 从 Firestore 获取时间戳数据。然后我想比较时间戳数据。 具体来说,Cloud Functions 使用 Cloud Scheduler 在每天 12:00 执行。我想检查firestore的时间戳在执行时是否超过10天。如果超过,则执行特定流程。

如果我可以在从 Firestore 获得的时间戳上加上 10 天,那将是可比的,但它不起作用。具体来说,我尝试使用 toDate() 将时间戳转换为日期,并尝试添加 10 天。

【问题讨论】:

标签: firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

Timestamp.valueOf() 方法是使用 >、= 和 > 运算符执行时间戳比较的最简单方法。但是,如果您正在处理向其中一个时间戳添加时间,那么比较秒数可能是最简单的方法:

const TEN_DAYS_IN_SECONDS = 60 * 60 * 24 * 10;
const timestamp_a = firestore.Timestamp.now();
const timestamp_b = firestore.Timestamp.now();

console.log(timestamp_a.seconds + TEN_DAYS_IN_SECONDS > timestamp_b.seconds);
// output: true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 2019-03-05
    • 2011-12-28
    • 2019-07-30
    • 2012-05-16
    • 2012-09-15
    • 2021-03-16
    • 1970-01-01
    相关资源
    最近更新 更多