Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent data type of cloud Firestore server timestamp in swift language?

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());
like image 485
Vahid Hallaji Avatar asked Dec 14 '22 16:12

Vahid Hallaji


1 Answers

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

like image 117
rbaldwin Avatar answered Apr 19 '23 23:04

rbaldwin