firebase - 如何比较 Firestore 时间戳对象
问题描述
使用带有 Cloud Scheduler 的 CloudFunctions 从 Firestore 获取时间戳数据。然后我想比较时间戳数据。具体来说,Cloud Functions 使用 Cloud Scheduler 在每天 12:00 执行。我想检查firestore的时间戳在执行时是否超过10天。如果超过,则执行特定流程。
如果我可以在从 Firestore 获得的时间戳上加上 10 天,那将是可比的,但它不起作用。具体来说,我尝试使用 toDate() 将时间戳转换为日期并尝试添加 10 天。
解决方案
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
推荐阅读
- html - 如何更改列宽?
- r - left_join 基于 R 中最接近的 LAT_LON
- c++ - 实例化模板时必须显式显示参数包中的类型吗?
- php - 尝试从 PHP 代码在 Oracle 中执行存储过程
- typescript - Nestjs无法解决依赖关系
- python - 根据 pandas 中的数据框格式化输出文本文件
- javascript - 如何使用复制按钮复制文本中的变量?
- mysql - 即使在执行存储过程“操作数应包含一列”之后也会出现无法理解的错误
- ignite - 如何最好地在 Apache ignite 缓存上持续更新有序数据
- python - 在 ml 中调用 fit() 后训练的数据存储在哪里?