【问题标题】:Get Timestamp from Firestore databse从 Firestore 数据库获取时间戳
【发布时间】:2021-05-19 12:13:22
【问题描述】:

我在我的 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 的那个)

谢谢!!

【问题讨论】:

    标签: javascript vue.js google-cloud-firestore timestamp


    【解决方案1】:

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

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

    比如:

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

    【讨论】:

    • 嗨弗兰克,谢谢!那是解决方案,但我仍然不知道为什么。
    • 它是用 Firestore 自己的格式编码的,它不是字符串,也不是 Javascript 日期对象。例如,它比 Javascript Date 对象具有更精细的“粒度”。提到的 .toMillis() 本质上将 Firestore 时间戳格式转换为 Javascript 日期对象格式。控制台在显示之前将其转换为显示。 firebase.google.com/docs/reference/js/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2021-06-05
    • 2018-10-05
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多