I've got a data model including a timestamp field createdAt
.
struct Box {
var title: String
var createdAt: Timestamp
var dictionary: [String : Any] {
return [
"title": title,
"createdAt": createdAt
]
}
// ...
However, I am unable to assign Firestore server timestamp. What would be the equivalent data type of cloud Firestore server timestamp in swift? Am I missing a point here?
let box = Box(title: title, createdAt: FieldValue.serverTimestamp());
When you read back the Timestamp
from Firestore
, you can convert it to a Date
object:
if let timestamp = data["createdAt"] as? Timestamp {
let date = timestamp.dateValue()
}
/** Returns a new NSDate corresponding to this timestamp. This may lose precision. */
- (NSDate *)dateValue;
The Timestamp
is an FIRTimestamp
object that returns like this:
FIRTimestamp: seconds=1525802726 nanoseconds=169000000>
After conversion with .dateValue()
:
2018-05-08 18:05:26 +0000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With