Display date from Firestore React native

550 views Asked by At

I try to display the picked event date för my posts from Firestore using moment. Right now it just print The current day, so I display the date but not ont he right way. I get no error message. I have tried to change the timestamp to "dateUpload". but it gives me a print in my Text component "invalid date". Do any have clue of what I can do?

This is how display it my flatlist:

postDate={moment(item.timestamp).format("ll")}

In my redux Action.js


export function fetchFollowingUsersPosts(uid) {
    return ((dispatch, getState) => {
        firebase.firestore()
            .collection("posts")
            .doc(uid)
            .collection("userPosts")
            .orderBy("creation", "asc")
            .get()
            .then((snapshot) => {

                const uid = snapshot.query.EP.path.segments[1];
                const user = getState().usersState.users.find(el => el.uid === uid);

                const posts = snapshot.docs.map((doc) => {
                    const { data: firebaseTimestamp, ...rest } = doc.data()
                    const id = doc.id;
                    const data = firebaseTimestamp ? moment(firebaseTimestamp.toDate()) : null
                  
                    return {
                      ...rest,
                      id,
                      user,
                      ...data
                    }
                  })

                //console.log(posts);
                dispatch({ type: USERS_POSTS_STATE_CHANGE, posts, uid,  })
            })
    })
}

Image on my database:

Image on database

1

There are 1 answers

6
dianaqqq On BEST ANSWER

Instead of const data = firebaseTimestamp ? moment(firebaseTimestamp.toDate()) : null, try this const data = firebaseTimestamp ? firebaseTimestamp.toDate() : null , so you'll be having a javascript Date object, instead of a Moment object. Then, you can use it as you did postDate={moment(item.timestamp).format("ll")}, assuming that item.timestamp is the Date object from above