如何将时间戳设置为日期


const docRef = await addDoc(collection(db, "question"), {
title: writingTitle,
createdAt: serverTimestamp(),
});

如果您在写入时写入servertimestamp((,控制台窗口显示{seconds=123123123,纳秒=!@!@}}如下。如何在网络上将其更改为"日期"?

要将Firestore时间戳转换为日期,请在从数据库读取值后对其调用toDate()。有关更多信息,请参阅参考文档。

在此之前/不读取该值是不可能获得的,因为serverTimestamp()只生成一个令牌值/ssentinel,数据库服务器将其识别为写入日期/时间的信号。

以下是我的操作方法(使用Typescript/Rect Native(:

const docRef = await addDoc(collection(db, "question"), {
title: writingTitle,
createdAt: serverTimestamp(),
});
const docSnapshot = await docRef.get();
//Then get any data from the added object
const timestamp = docSnapshot.data().createdAt

最新更新