首页 > 解决方案 > 从 Firestore 数据库获取时间戳

问题描述

我在我的 Firestore 数据库中存储了一个时间戳,但是当我想获取时间戳时,我得到了一些我无法解释的数字。也许有一种特殊的技术可以从我不知道的 vuejs 应用程序中的 firestore 获取时间戳。

我的时间戳如下所示:

时间戳

但是,如果我输入以下命令来获取我的时间戳:

let ref = db.collection('profile').doc('profile_data')
ref.get().then(snapshot => {
  profile = snapshot.data()
  profile.id = snapshot.id
}).then( () => {
  console.log(profile.date_of_birth)
})

我得到以下输出: 063147682800.000000000 与时间戳无关。有人知道如何从我的 Firestore 数据库中获取时间戳,以便我得到以下结果:1012086000000(这是我用来设置 date_of_birth 的那个)

谢谢!!

标签: javascriptvue.jsgoogle-cloud-firestoretimestamp

解决方案


您看到的实际上是 Firestore 的Timestampclass的内部表示。

如果您想获取自纪元以来的毫秒值,您可以调用toMillis()时间戳对象。

所以像:

console.log(profile.date_of_birth.toMillis())

推荐阅读