I would like to convert a timestamp posted in firebase to readable data. I am able to read the timestamp, but not able to convert it, or append it to an array.
This is my progress:
func getOrderDates() { let uid = Auth.auth().currentUser!.uid let orderDateHistoryRef = Database.database().reference().child("users/\(uid)/Orders/") orderDateHistoryRef.observeSingleEvent(of: .value, with: { (snapshot) in let value = snapshot.value as? NSDictionary if let orderDate = value?["Date"] as? [Int:String] { self.orderDateHistoryArray += Array(orderDate.values).map{ Date(timeIntervalSince1970: TimeInterval($0/1000))} // This fails. Error: "Binary operator '/' cannot be applied to operands of type 'String' and 'Int'" print(orderDate) } self.tableView.reloadData() // ... }) { (error) in print(error.localizedDescription) } } }
The print(orderDate) statement prints correctly:
["-LQYspEghK3KE27MlFNE": 1541421618601,
“-LQsYbhf-vl-NnRLTHhK”: 1541768379422,
“-LQYDWAKlzTrlTtO1Qiz”: 1541410526186,
“-LQsILjpNqKwLl9XBcQm”: 1541764115618]
This is
childByAutoID
: timeInMilliseconds
So, I want to read out the
timeInMilliseconds
, convert it to a readable timestamp
and append it to the orderDateHistoryArray
Seems you are very near to what you want.
As the error message is clearly showing, your updated line 17. is a code to convert a single `Date` to `String`.
Not to convert `[Date]`.
You can use `map` as in your line 11.
let readableDates = dates.map {formatter.string(from: $0)} self.orderDateHistoryArray = readableDates