How to insert a firebase server timestamp using the iOS SDK in Swift project

Add the ".sv":"timestamp" between brackets [ ] when you add it to your Firebase entry.

eg:

let objectToSave: Dictionary <String: AnyObject> = ["date": [".sv": "timestamp"], "key1": "value1", "key2": 1234.....] 

Code screenshot

It should be written to your Firebase as:

date: 1463329843759

Firebase screenshot


As of Firebase 4.0 you can use ServerValue.timestamp()

for example:

    let ref = Database.database().reference().child("userExample")
    let values = ["firstName": "Joe", "lastName": "Bloggs", "timestamp": ServerValue.timestamp()] as [String : Any]

    ref.updateChildValues(values) { (err, ref) in

        if let err = err {
            print("failed to upload user data", err)
            return
        }

    }