Firebase Firestore时间戳记到格式化日期

时间:2019-11-26 06:49:40

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

我正在研究一个Firebase项目,并且为了显示有关实体的时间,我得到的是时间戳记。我正在做一个React Native项目。如何将接收到的时间戳转换为正确格式的日期。 这是时间戳记:

enter image description here

我这次在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))

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

如果您从文档中检索数据,则可以执行以下操作:

// 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 :(得分:0)

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

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

因为, enter image description here