我有一个云函数,它重置上文档字段的日期,但是将时间戳的小时段设置为午夜。
这将在每天午夜从客户端重置,在客户端触发云函数本身。
然而,云函数将日期设置为比预期日期提前一小时。也就是说,如果实际日期是2022年3月29日00:00 UTC+1,云函数将其设置为2022年3月29日01:00 UTC+1。
我不能使用服务器端时间戳,因为这不允许通过云函数向其添加任何时间,这对于我的用例来说是必要的,因为我有时会在函数的后面部分重置日期一周。
在昨天更改UTC +1之前,云功能运行正常,并将日期更新到预期的日期和时间(即午夜)。
到目前为止,我把这一天设定为午夜:
const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());然后,我通过以下方式更新该文档:
return ref.doc(doc.id).update({
"Next Date Due": admin.firestore.Timestamp.fromDate(today)
});发布于 2022-03-29 05:58:36
Fi还原时间戳中没有编码时区。它只是使用秒和纳秒(这些是时间戳对象上的字段)来存储来自Unix时代的偏移量。
如果您在Firestore中查看时间戳字段,您将看到计算机从其本地设置中使用的本地时区中显示的时间。例如,我的系统上有一个本地时区,即UTC +8,如果我更新对象字段"Next Date Due",Firestore将显示March 29, 2022 at 8:00:00 AM UTC+8,因为它反映了系统上的时区,如下面的屏幕截图所示。

您可以尝试获取更新后的时间,以进行双重检查:
const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());
console.log('today:', today);
ref.doc(doc.id).update({
"Next Date Due": admin.firestore.Timestamp.fromDate(today)
})
.then(() => {
ref.doc(doc.id).get().then((doc) => {
if (doc.exists) {
console.log("Document data:", doc.data()["Next Date Due"].toDate());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch((error) => {
console.log("Error getting document:", error);
});
});这将导致:
today: 2022-03-29T00:00:00.000Z
Document data: 2022-03-29T00:00:00.000Z如果您希望呈现特定时区的日期对象,我建议您使用库(如moment.js )。
https://stackoverflow.com/questions/71654784
复制相似问题