0

Here's the problem I have: Some smart guy saved timestamps in our database as strings. So dates for created documents look like this: 1569260406765

Here's my code:

tasksRef.getDocuments { (querySnapshot, err) in
    if let docs = querySnapshot?.documents {
        for docSnapshot in docs {
            print("Snapshot:" ,docSnapshot.data())
            let dateCreated = docSnapshot.get("dateCreated")
            let date = NSDate(timeIntervalSince1970: dateCreated as! TimeInterval)

            print("dateCreated:", date)
            self.recentOrderNowsFirst.text = "\(String(describing: dateCreated))"

        }
    }
} 

which gets me this error:

Could not cast value of type 'FIRTimestamp' (0x101df5660) to 'NSNumber'

How can I fix this? Appreciate any help.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Marco
  • 21
  • 4
  • It looks to me like the smart guy actually use a timestamp type field for dateCreated, not a string. That's what FIRTimestamp refers to. https://firebase.google.com/docs/reference/swift/firebasefirestore/api/reference/Classes/Timestamp – Doug Stevenson Sep 24 '19 at 03:40
  • Possible duplicate of [Convert Firebase Firestore Timestamp to Date (Swift)?](https://stackoverflow.com/questions/51116381/convert-firebase-firestore-timestamp-to-date-swift) – Kamran Sep 24 '19 at 05:06
  • yes it seems like he used a FIRTimestamp, but then in the database he saved it as a number or string and I have no idea how to convert it to a readable format? – Marco Sep 24 '19 at 22:53

0 Answers0