我有一个数据模型,包括一个时间戳字段createdAt
。
struct Box {
var title: String
var createdAt: Timestamp
var dictionary: [String : Any] {
return [
"title": title,
"createdAt": createdAt
]
}
// ...
但是,我无法分配 Firestore 服务器时间戳。在 swift 中,云 Firestore 服务器时间戳的等效数据类型是什么?我在这里错过了一点吗?
let box = Box(title: title, createdAt: FieldValue.serverTimestamp());
当您从Firestore
中读回Timestamp
时,您可以将其转换为Date
对象:
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;
Timestamp
是一个FIRTimestamp
对象,返回如下:
FIRTimestamp: seconds=1525802726 nanoseconds=169000000>
使用.dateValue()
转换后:
2018-05-08 18:05:26 +0000