首先,您必须使用时间戳属性存储 Firestore 文档,然后使用 Flutter 的 DateTime 和 DateFormat API 查询带有时间戳值的文档。
一旦您从 Firestore 获得时间戳,类似于:
时间戳(秒=1560523991,纳秒=286000000)
您可以使用 2 种方式从时间戳值中获取四舍五入的小时数:
你需要把它解析成一个DateTime类型的对象:
DateTime myDateTime = (snapshot.data.documents[index].data['timestamp']).toDate(); // prints 2020-05-09 15:27:04.074
这将以 dart 的 DateTime 格式返回您的 Firestore 时间戳。为了转换您的 DateTime 对象,您可以使用 intl 包中的 DateFormat 类。您可以使用获得的 DateTime 对象来获取您选择的格式,如下所示:
可能的解决方案 1:
DateFormat.Hm().format(myDateTime); //prints 15.27
所以查询应该查找具有以下内容的文档:
DateFormat.m().format(myDateTime) to be “00”作为
DateFormat.m() returns minutes and if minutes == 00 then the timestamp is rounded to the nearest hour.
可能的解决方案 2:
使用 : 将时间戳转换为时间字符串:
Let TimeString = snapshot.data.documents[index].data['timestamp']).toDate().toString() // prints ‘2019-12-28 18:48:48.364’
Let time = TimeString.split(“ “)[1] // prints 18:48:48.364
To check if the minutes is “00” :
if time.substring(3,5) == “00”, then it's a rounded hour. //here it is 48
您必须输入这些时间戳转换逻辑,然后按照我提到的四舍五入的小时时间戳进行查询。没有现成的可用功能/方法来获取具有四舍五入时间的 Firestore 文档。