I am trying to figure out how to display a firestore timestamp in a react app.
I have a firestore document with a field named createdAt.
I am trying to inc
After some discussion, we found that the time stamps in OPs user object could be renders as such:
render() {
const { users, loading } = this.state;
return (
{loading && Loading ...}
{users.map(user => (
{user.email}
{user.name}
{new Date(user.createdAt.seconds * 1000).toLocaleDateString("en-US")}
I recreated your example in a dummy React project, and received the same error as expected.
Error: Objects are not valid as a React child
I was able to get this to render correctly with the following method, which should also work for you:
{new Date(user.createdAt._seconds * 1000).toLocaleDateString("en-US")}
Which, for my sample timestamp, rendered as:
12/30/2019
Be sure you are using a timestamp that was saved to Firestore as:
createdAt: this.props.firebase.Timestamp.fromDate(new Date())
Note: This is assuming that your instance of firebase.firestore() is at this.props.firebase. In other examples you use this.props.firebase, but those methods look like helper methods that you have created yourself.
When this value is fetched, it will be an object with two properties -- _seconds and _nanoseconds.
Be sure to include the underscore. If you use createdAt.seconds it won't work, it must be createdAt._seconds.
Other things I tried:
user.createdAt.toDate() throws toDate() is not a function.
user.createdAt throws Error: Objects are not valid as a React child
new Date(user.createdAt._nanoseconds) renders the wrong date