【问题标题】:Firebase firestore timestamp to Formatted DateFirebase firestore 时间戳到格式化日期
【发布时间】:2020-03-21 12:51:36
【问题描述】:

我正在做一个 firebase 项目,为了显示时间和实体,我得到的是时间戳。我正在做一个反应原生项目。如何将收到的时间戳转换为格式正确的日期。 这是时间戳:

我在this.props.time 有这个时间,所以我尝试了this.props.time.toDate(),但我得到的是一个错误。 Invariant Violation:Objects are not valid as a React child(found: Tue Nov 26 2019 11:51:58 GMT+05:30(Indian Standard Time))

【问题讨论】:

  • 我猜你可以把它转换成类似new Date(this.props.time.seconds * 1000)
  • 我认为它是正确的,但显示它时出错。你能提供代码你是如何显示它的吗?
  • @KishanBharda 已修复!我需要将其转换为字符串。我所做的是:new Date(this.props.time.seconds * 1000 + this.props.time.nanoseconds / 1000,).toLocaleDateString()

标签: javascript firebase react-native google-cloud-firestore redux-firestore


【解决方案1】:

您可以使用new Date(time.seconds * 1000 + time.nanoseconds/1000000) 创建一个Date object,然后按照您想要的方式对其进行操作。

【讨论】:

  • 在我这样做后它起作用了:new Date(this.props.time.seconds * 1000 + this.props.time.nanoseconds / 1000, ).toLocaleDateString()
【解决方案2】:

如果您从文档中检索数据,您可以这样做:

// time.toDate()
convertDate(doc.data().time.toDate())

const convertDate = (date) => {
    // whatever formatting you want to do can be done here
    var d = date.toString()
    return d.substr(0, 21);
}

【讨论】:

    【解决方案3】:

    可以使用https://momentjs.com/显示

    {moment(yourTimestamp.toDate()).format( ****格式在上面的链接中 )}

    【讨论】:

      【解决方案4】:

      正确: new Date(time.seconds * 1000 + time.nanoseconds/1000000)

      不正确: new Date(time.seconds * 1000 + time.nanoseconds/1000)

      因为,

      【讨论】:

        猜你喜欢
        • 2018-06-15
        • 2021-04-14
        • 2020-02-23
        • 2020-02-28
        • 2019-10-30
        • 1970-01-01
        • 2020-12-06
        • 1970-01-01
        • 2018-12-09
        相关资源
        最近更新 更多