gpt4 book ai didi

flutter - 如何在 Firestore 中使用时间戳值查询一整天

转载 作者:行者123 更新时间:2023-12-02 01:31:06 26 4
gpt4 key购买 nike

我有一个 firestore 集合“orders”,其文档具有时间戳值。我想根据时间戳过滤该集合的文档。例如,过滤 2022 年 7 月 1 日下的订单。我将从 Datepicker 获取的日期值作为 DateTime 传递。我形成的查询是

_db.collection('orders')
.where('driverId', isEqualTo: sp.getString('uid'))
.where('timeStamp', isGreaterThanOrEqualTo: Timestamp.fromDate(pickedDate!))
.get().then((querySnapshot){
querySnapshot.docs.forEach((element) {
orders.add(element.data());
});
print(orders.length);
});

问题是,自从我给出了 isGreaterThanOrEqualTo 以来,我从 7 月 1 日到今天都收到了订单。但是如果我给出isEqualTo,它什么也不会返回。我猜这意味着需要01-07-2022 00:00:00。那么在选定日期获取订单的正确查询是什么

最佳答案

Firestore 中的

Timestamp 对象是非常具体的时间点,具有微秒精度。因此,如果您想匹配一整天的所有时间戳,那就是时间戳值的范围

典型的方法是创建一天后的时间戳,然后将其添加到范围所需的第二个子句中:

_db.collection('orders')
.where('driverId', isEqualTo: sp.getString('uid'))
.where('timeStamp', isGreaterThanOrEqualTo: Timestamp.fromDate(pickedDate!))
.where('timeStamp', isLessThan: timestampOfStartOfNextDay)

关于flutter - 如何在 Firestore 中使用时间戳值查询一整天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73259128/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com