Add timestamp in Firestore documents

前端 未结 10 1008
旧巷少年郎
旧巷少年郎 2020-12-29 03:29

I\'m newbie to Firestore. Firestore docs says...

Important: Unlike \"push IDs\" in the Firebase Realtime Database, Cloud Firestore au

相关标签:
10条回答
  • 2020-12-29 04:20

    For Firestore

    ref.doc(key).set({
      created: firebase.firestore.FieldValue.serverTimestamp()
    })
    
    0 讨论(0)
  • 2020-12-29 04:24

    Use firestore Timestamp class, firebase.firestore.Timestamp.now().

    Since firebase.firestore.FieldValue.serverTimestamp() does not work with add method from firestore. Reference

    0 讨论(0)
  • 2020-12-29 04:28

    Try this one for Swift 4 Timestamp(date: Date())

    let docData: [String: Any] = [
    "stringExample": "Hello world!",
    "booleanExample": true,
    "numberExample": 3.14159265,
    "dateExample": Timestamp(Date()),
    "arrayExample": [5, true, "hello"],
    "nullExample": NSNull(),
    "objectExample": [
        "a": 5,
        "b": [
            "nested": "foo"
        ]
    ]
    ]
    db.collection("data").document("one").setData(docData) { err in
    if let err = err {
        print("Error writing document: \(err)")
    } else {
        print("Document successfully written!")
    }
    }
    
    0 讨论(0)
  • 2020-12-29 04:30

    Firestone method does not work. Use Timestamp from java.sql.Timestamp and don't cast to string.. Then firestone formats it properly. For example to mark a now() use:

    val timestamp = Timestamp(System.currentTimeMillis())
    
    0 讨论(0)
提交回复
热议问题