我有一个包含时间戳字段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());
答案 0 :(得分:7)
当您从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;
Timestamp
是一个FIRTimestamp
对象,返回如下:
FIRTimestamp: seconds=1525802726 nanoseconds=169000000>
转换为.dateValue()
后:
2018-05-08 18:05:26 +0000