如何从firestore的FieldValue.serverTimestamp()中减去24小时



我需要将两个值传递给Firebase Cloud Function

第一个是current Timestamp,第二个是Timestamp24 hrs背面。

我使用FieldValue.serverTimestamp()获取当前时间戳,但如何从FieldValue.serverTimestamp()中减去24小时

我需要与FieldValue.serverTimestamp()格式相同的最终结果。

我的整个代码都在java 中

如果有另一个解决方案,请参考它,但主要是我想在服务器时间戳上执行操作

感谢您的帮助,但找到了替代解决方案。我不确定这是正确的方式,但它对我有效。

Date currentTime = new Date(Timestamp.now().getSeconds() * 1000);

Date currentTime24hrsBack = new Date(Timestamp.now().getSeconds() * 1000 - 24 * 60 * 60 * 1000);

new Timestamp(currentTime24hrsBack).toDate()

new Timestamp(currentTime24hrsBack).toDate()

import com.google.firebase.Timestamp;

无法直接对FieldValue.serverTimestamp()执行日期计算。它返回一个静态令牌值(根本不是时间戳对象(,该值在写入时在服务器上进行解释。

如果你想计算相对于服务器时间戳值的一些时间,你必须:

  1. 编写FieldValue.serverTimestamp()
  2. 将其作为时间戳对象从文档中读回
  3. 对时间戳的组件值(秒(执行时间计算,以创建具有所需时间的新时间戳
  4. 将新的时间戳写入文档

最新更新