Mic*_*siO 1 javascript firebase google-cloud-functions google-cloud-firestore
我如何从时间戳中减去一周firebase.firestore.Timestamp.now()?我有不同的带有时间戳的文档,我需要构建一个函数来检查这些文档中是否有任何文档的时间戳早于 7 天。哎呀,也许有一个内置函数可以检查日期是否“过期”?提前致谢。
正如这里所定义的,
firebase.firestore.Timestamp.now()
Run Code Online (Sandbox Code Playgroud)
相当于
firebase.firestore.Timestamp.fromMillis(Date.now())
Run Code Online (Sandbox Code Playgroud)
要获取一周前的时间戳,您可以使用:
// 1 week in ms = 1000 * 60 * 60 * 24 * 7 = 604800000
const nowTimestamp = firebase.firestore.Timestamp.now();
const weekOldTimestamp = firebase.firestore.Timestamp.fromMillis(nowTimestamp.toMillis() - 604800000);
// or the shorter
const weekOldTimestamp = firebase.firestore.Timestamp.fromMillis(Date.now() - 604800000);
Run Code Online (Sandbox Code Playgroud)
假设您在名为 的集合中有以下文档/photos:
firebase.firestore.Timestamp.now()
Run Code Online (Sandbox Code Playgroud)
如果您想查找一周以上的所有照片,您可以使用以下任一相同语句:
firebase.firestore().collection("photos")
.where("createdAt", "<", new Date(Date.now() - 604800000))
.get()
Run Code Online (Sandbox Code Playgroud)
或者
const weekAgoTimestamp = firebase.firestore.Timestamp.fromMillis(Date.now() - 604800000);
firebase.firestore().collection("photos")
.where("createdAt", "<", weekAgoTimestamp)
.get()
Run Code Online (Sandbox Code Playgroud)
如果您已经拥有该文档的副本,请通过如下查询:
const aPhotoSnapshot = await firebase.firestore()
.doc("photos/somePhotoId")
.get();
Run Code Online (Sandbox Code Playgroud)
如果你想检查它是否超过一周,你可以使用:
const createdAtTimestamp = aPhotoSnapshot.get("createdAt");
const isWeekOld = createdAtTimestamp.toMillis() < Date.now() - 604800000;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1287 次 |
| 最近记录: |