我已经将包含用户日期和时区的createdAt字段存储在Firestore文档中。例如(2019年12月22日晚上9:21:42,协调世界时-3)
然后,我有一个云函数,在这里我想检索所有的今天文档。问题是云函数不知道Timezone,当检索不同时区的文档时,这会变得很混乱。
const now: Date = new Date();
const docQuery = await db
.collection('users')
.where('createdAt', '>', new Date(now.getFullYear(), now.getMonth(), now.getDate()))
.where('createdAt', '<', new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59))
.get();now日期以UTC-0为单位,如果我有前一天22:00 UTC-3的文档,查询将检索它,但在技术上它不是“今日”文档。
对如何解决这个问题有什么想法吗?
发布于 2019-12-23 14:50:15
在防火墙文档中使用timestamp而不是日期。
ref.doc(key).set({
created : firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815 19:00:00 EST"))
}) 时间戳表示与任何时区或日历无关的时间点,以秒和秒的分数表示,在UTC时代,秒的分辨率为纳秒。
然后,可以使用toDate()方法将时间戳转换为Date对象。
https://stackoverflow.com/questions/59456835
复制相似问题