首页 > 解决方案 > Kotlin:将当前日期存储在 Firestore 中作为时间戳

问题描述

不知道为什么我找不到我要找的东西。但我只想将当前时间存储到这样的 Firestore 文档中:

在此处输入图像描述

. 在 Flutter 中,我们只是这样做:

'timestamp': DateTime.now()

我尝试在 kotlin 中这样做:

"timestamp" to LocalDateTime.now()

但它给了我一些复杂的领域:

在此处输入图像描述

标签: kotlingoogle-cloud-firestore

解决方案


在所有平台上最好的方法是使用 SDK 提供的服务器时间戳令牌(而不是使用客户端的时钟,这可能是错误的)。

文档在这里

// Update the timestamp field with the value from the server
val updates = hashMapOf<String, Any>(
        "timestamp" to FieldValue.serverTimestamp()
)

docRef.update(updates).addOnCompleteListener { }

如果您确实需要客户端时钟的时间,只需传递 java Date withDate()Timestamp with Timestamp.now()


推荐阅读