我有一个包含时间戳字段的数据模型 createdAt 。
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());
当你读回 Timestamp 从…起 Firestore ,可以将其转换为 Date 对象:
Timestamp
Firestore
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;
这个 时间戳 是一个 FIRTimestamp 返回如下内容的对象:
时间戳
FIRTimestamp
FIRTimestamp: seconds=1525802726 nanoseconds=169000000>
转换后使用 .dateValue() :
.dateValue()
2018-05-08 18:05:26 +0000