Javascript 从firebase.firestore.Timestamp.now()中减去一周?

Javascript 从firebase.firestore.Timestamp.now()中减去一周?,javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,如何从firebase.firestore.Timestamp.now()中减去一周?我有不同的文档有时间戳,我需要构建一个函数来检查这些文档中是否有时间戳超过7天的文档。见鬼,也许有一个内置函数来检查日期是否“过期”?提前感谢。根据定义 相当于 firebase.firestore.Timestamp.fromMillis(Date.now()) 要获取一周前的时间戳,您可以使用: // 1 week in ms = 1000 * 60 * 60 * 24 * 7 = 604800000

如何从
firebase.firestore.Timestamp.now()中减去一周?我有不同的文档有时间戳,我需要构建一个函数来检查这些文档中是否有时间戳超过7天的文档。见鬼,也许有一个内置函数来检查日期是否“过期”?提前感谢。

根据定义

相当于

firebase.firestore.Timestamp.fromMillis(Date.now())
要获取一周前的时间戳,您可以使用:

// 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);
const createdAtTimestamp = aPhotoSnapshot.get("createdAt");
const isWeekOld = createdAtTimestamp.toMillis() < Date.now() - 604800000;
假设您在名为
/photos
的集合中有以下文档:

{
“标题”:“我的照片”,
“描述”:“这是我在这里上传的第一张照片”,
“storageRef”:“/userData/userid1/uploads/89q24u2q98y23.png”,
“thumbnailRef”:“/userData/userid1/uploads/89q24u2q98y23.thumbnail.png”,
“createdAt”:FieldValue.serverTimestamp(),
}
如果要查找所有超过一周的照片,请使用以下相同语句之一:

firebase.firestore().collection("photos")
  .where("createdAt", "<", new Date(Date.now() - 604800000))
  .get()
如果您想检查它是否超过一周,您可以使用:

// 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);
const createdAtTimestamp = aPhotoSnapshot.get("createdAt");
const isWeekOld = createdAtTimestamp.toMillis() < Date.now() - 604800000;
const createdAtTimestamp=aPhotoSnapshot.get(“createdAt”);
const isWeekOld=createdAtTimestamp.toMillis()
根据定义

相当于

firebase.firestore.Timestamp.fromMillis(Date.now())
要获取一周前的时间戳,您可以使用:

// 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);
const createdAtTimestamp = aPhotoSnapshot.get("createdAt");
const isWeekOld = createdAtTimestamp.toMillis() < Date.now() - 604800000;
假设您在名为
/photos
的集合中有以下文档:

{
“标题”:“我的照片”,
“描述”:“这是我在这里上传的第一张照片”,
“storageRef”:“/userData/userid1/uploads/89q24u2q98y23.png”,
“thumbnailRef”:“/userData/userid1/uploads/89q24u2q98y23.thumbnail.png”,
“createdAt”:FieldValue.serverTimestamp(),
}
如果要查找所有超过一周的照片,请使用以下相同语句之一:

firebase.firestore().collection("photos")
  .where("createdAt", "<", new Date(Date.now() - 604800000))
  .get()
如果您想检查它是否超过一周,您可以使用:

// 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);
const createdAtTimestamp = aPhotoSnapshot.get("createdAt");
const isWeekOld = createdAtTimestamp.toMillis() < Date.now() - 604800000;
const createdAtTimestamp=aPhotoSnapshot.get(“createdAt”);
const isWeekOld=createdAtTimestamp.toMillis()
取决于精度,但您可以从时间戳中提取秒数并减去一周(604800)?或者使用内置的
toDate()
方法和类似date fns的库来比较javascript日期对象?取决于精度,但您可以从时间戳中提取秒数并减去一周(604800)?或者使用内置的
toDate()
方法和类似date fns的库来比较javascript日期对象?