Firestore中通过TimeStamp通过和传递项目以进行S

Firestore中通过TimeStamp通过和传递项目以进行S

本文介绍了在Firestore中通过TimeStamp通过和传递项目以进行Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用程序的Firestore Timestamp中传递今天的日期.我今天是这样的日期

I am trying to pass today's date in Firestore Timestamp in my app. I am getting the today's date like this

formatter.dateFormat = "MMMM d, yyyy"
let result = formatter.string(from: date)
print("Today date is \(result)")
let startDate = result + " " + "07:00:00"
let endDate = result + " " + "23:00:00"

我必须将今天的日期从早上7点传送到23:00 pm,并将此日期存储在startDateendDate中.现在,我在Firestore查询中传递这些计时,如下所示:-

I have to pass the today's date from morning 7 am to 23:00 pm and I stored this dates in startDate and endDate. Now I am passing these timing in Firestore query like this:-

self.db.collection("Locations").whereField("userid", isEqualTo: "\(selectedUserID)").whereField("createddatetime", isGreaterThanOrEqualTo: "\(startDate)").whereField("createddatetime", isLessThanOrEqualTo: "\(endDate)").getDocuments()

但是我的快照没有任何结果.我怎样才能做到这一点.请帮忙吗?

but I am getting no results in my snapshot. How can I do this. Please help?

推荐答案

您需要:

1)转换字符串->日期

1) Convert String -> Date

2)转换日期->时间戳(FIRTimestamp)

2) Convert Date -> Timestamp (FIRTimestamp)

formatter.dateFormat = "MMMM d, yyyy HH:mm:ss"

let startTime: Date = formatter.date(from: startDate) ?? Date(timeIntervalSince1970: 0)
let startTimestamp: Timestamp = Timestamp(date: startTime)

let endTime: Date = formatter.date(from: endDate) ?? Date()
let endTimestamp: Timestamp = Timestamp(date: endTime)

3)将func更改为:

3) Change func to:

self.db.collection("Locations")
        .whereField("userid", isEqualTo: "\(selectedUserID)")
        .whereField("createddatetime", isGreaterThanOrEqualTo: startTimestamp)
        .whereField("createddatetime", isLessThanOrEqualTo: endTimestamp)
        .getDocuments() { (snapshot, error) in

        ...

}

这篇关于在Firestore中通过TimeStamp通过和传递项目以进行Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:09