我正在尝试在应用中的Firestore Timestamp
中传递今天的日期。我今天是这样的日期
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之间的今天日期作为日期,并将此日期存储在startDate
和endDate
中。现在,我在Firestore
查询中传递这些计时,如下所示:-
self.db.collection("Locations").whereField("userid", isEqualTo: "\(selectedUserID)").whereField("createddatetime", isGreaterThanOrEqualTo: "\(startDate)").whereField("createddatetime", isLessThanOrEqualTo: "\(endDate)").getDocuments()
但是我的快照没有任何结果。我怎样才能做到这一点。请帮忙吗?
答案 0 :(得分:3)
您需要:
1)转换字符串->日期
2)转换日期->时间戳(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)将功能更改为:
self.db.collection("Locations")
.whereField("userid", isEqualTo: "\(selectedUserID)")
.whereField("createddatetime", isGreaterThanOrEqualTo: startTimestamp)
.whereField("createddatetime", isLessThanOrEqualTo: endTimestamp)
.getDocuments() { (snapshot, error) in
...
}