簡體   English   中英

Kotlin:將當前日期存儲在 Firestore 中作為時間戳

[英]Kotlin: Store current date in Firestore as timestamp

不知道為什么我找不到我要找的東西。 但我只想將當前時間存儲到這樣的 Firestore 文檔中:

在此處輸入圖像描述

. 在 Flutter 中,我們只是這樣做:

'timestamp': DateTime.now()

我嘗試在科特林中這樣做:

"timestamp" to LocalDateTime.now()

但它給了我一些復雜的領域:

在此處輸入圖像描述

在所有平台上最好的方法是使用 SDK 提供的服務器時間戳令牌(而不是使用客戶端的時鍾,這可能是錯誤的)。

文檔在這里

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

docRef.update(updates).addOnCompleteListener { }

如果您真的想要客戶端時鍾的時間,只需傳遞一個帶有Date()的 java Date 或帶有Timestamp.now Timestamp.now()的 Timestamp。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM