Firebase:将实时数据库时间戳转换为firestore时间戳的代码/公式



我正在Firebase上运行一个web应用程序,开始使用实时数据库,现在我正在将数据迁移到firestore。

我使用adminSDK迁移了除时间戳之外的所有其他数据。在我的web应用程序中,当用户上传新内容时,代码会将时间戳添加到数据库中,以便可以按时间对内容进行排序。

问题是,实时数据库和firestore在记录时间戳时显然使用了不同的方法:

firebase.database.ServerValue.TIMESTAMP // This will be in the following format: 1577778747131 
firebase.firestore.Timestamp.now() // This will be in the following format: December 31, 2019 at 4:52:00 PM UTC + 9

另一个问题是firebase.firestore.Timestamp.now((显然不是以毫秒或纳秒为单位存储的,而是以系统中的实际日期和时间为单位存储。所以,我不知道在写firestore而不是firebase.firestore.Timestamp.now((.时,应该提供什么值(如果有的话(

在Javascript中,将时间戳值从实时数据库转换为firebase时间戳的代码是什么?基本上,在下面的空白处需要填写什么代码:

postinfo.forEach(pi=>{
const a = pi.time; //Timestamp stored  in realtime database format.
/*
Code to transform the timestamp in realtime database to the format to be uploaded to firestore. This I don't know yet.
*/
const b = new_time;
// Some code to log the new time instead of the old time. This I can take care of.

我可以花时间使用在线可用的转换器之一,并以新格式手动记录时间,但这需要几个小时,这不是程序员想要做的事情。关于如何做到这一点,有什么建议吗?

您将在实时数据库中指定为ServerValue.timestamp的服务器时间戳在Firestore中称为FieldValue.serverTimestamp((.

Firestore中的时间戳不像Realtime Database那样以毫秒为单位存储为unix时间。它们使用两个数字存储,偏移量以秒和纳秒为单位,如时间戳的API文档中所述。从Firestore中读取时间戳类型字段时,请使用该Timestamp对象。

在本文中阅读有关Firestore服务器时间戳如何工作的更多信息。

最新更新