Pass and filter items by TimeStamp in Firestore for Swift

前端 未结 1 746

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\"         


        
相关标签:
1条回答
  • 2021-01-21 05:30

    You need:

    1) Convert String -> Date

    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) Change func to:

    self.db.collection("Locations")
            .whereField("userid", isEqualTo: "\(selectedUserID)")
            .whereField("createddatetime", isGreaterThanOrEqualTo: startTimestamp)
            .whereField("createddatetime", isLessThanOrEqualTo: endTimestamp)
            .getDocuments() { (snapshot, error) in
    
            ...
    
    }
    
    0 讨论(0)
提交回复
热议问题