Javascript Cloud Firestore:过滤今天的文档';云函数中的日期
我在Firestore文档中存储了createdAt字段,其中包含用户的日期和时区。例如(2019年12月22日UTC-3下午9:21:42) 然后,我有一个云函数,我想在其中检索今天所有的文档。问题是云函数不知道时区,在检索不同时区的文档时会变得混乱Javascript Cloud Firestore:过滤今天的文档';云函数中的日期,javascript,firebase,date,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Date,Google Cloud Firestore,Google Cloud Functions,我在Firestore文档中存储了createdAt字段,其中包含用户的日期和时区。例如(2019年12月22日UTC-3下午9:21:42) 然后,我有一个云函数,我想在其中检索今天所有的文档。问题是云函数不知道时区,在检索不同时区的文档时会变得混乱 const now: Date = new Date(); const docQuery = await db .collection('users') .where('createdAt', '>', new Date(no
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();
const now:Date=new Date();
const docQuery=wait db
.collection('用户')
.where('createdAt','>',新日期(现在是.getFullYear(),现在是.getMonth(),现在是.getDate())
.where('createdAt','在firestore文档中使用时间戳
,而不是日期
ref.doc(key).set({
created : firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815 19:00:00 EST"))
})
时间戳表示独立于任何时区或日历的时间点,在UTC历元时间中以纳秒分辨率表示为秒和秒分数
然后,您可以使用该方法将时间戳转换为日期对象。如果用户选择了日期时间,我该怎么做?请检查此var docData={stringExample:“Hello world!”,booleanExample:true,numberExample:3.14159265,dateExample:firebase.firestore.timestamp.fromDate(新日期(“1815年12月10日”)),arrayExample:[5,true,“hello”],nullExample:null,objectExample:{a:5,b:{nested:“foo”}};db.collection(“data”).doc(“one”).set(docData)。然后(function(){console.log(“Document successfully writed!”;});
So dofirebase.firestore.Timestamp.fromDate(新日期(“1815年12月10日”)),
将忽略日期时区?Firestore服务器时间戳在查询时没有帮助。它们仅用于将字段值设置为当前时刻。查询需要通过日期对象或时间戳对象提供查询的精确时刻。您必须初始化Da使用您想要的特定时区创建对象。或者,您必须使用其他库来帮助。我理解,因此适当的解决方案是,前端将参数发送到具有设备所需时区的去云函数,并使用该参数创建查询日期,对吗?谢谢@Dougstevenson是的,您会的需要从定义“日”的人那里获取时区(或转换为日期的特定日期时间,以毫秒为单位)。