首页 > 解决方案 > Firestore 如何保存日期?

问题描述

我最近发现了firestore.Timestamp,所以我试图分配firestore.FieldValue.serverTimestamp();给一个,但它没有用。相反,它说Type 'FieldValue' is not assignable to type 'Timestamp'我会说这很奇怪。不应该serverTimestamp()是 aTimestamp而不是 aFieldValue吗?firestore.Timestamp如果不将时间戳保存到 Firestore ,目的是什么?有没有办法获得一个与时间戳兼容的服务器时间戳,firestore.Timestamp或者我应该完全避免时间戳,并且只坚持firestore.FieldValue我所有的日期?

标签: firebasetimestampgoogle-cloud-firestore

解决方案


serverTimestamp()只返回哨兵值,告诉 Firestore 服务器它应该使用服务器的当前时间作为您尝试设置的字段的值。它不返回实际的 Timestamp 对象本身。

您使用它的原因是确保在服务器上设置的日期一致,而不是取决于用户设备上的时钟是否正确。

如果您想知道设备上的当前时间,只需使用语言或操作系统提供的本机日期/时间对象。


推荐阅读