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
How to send the date to Firestore:
import firebase from 'firebase/app';
// ..........
// someObject is the object you're saving to your Firestore.
someObject.createdAt: firebase.firestore.Timestamp.fromDate(new Date())
How to read it back:
function mapMonth(monthIndex) {
const months = {
0: 'jan',
1: 'feb',
2: 'mar',
3: 'apr',
4: 'may',
5: 'jun',
6: 'jul',
7: 'aug',
8: 'sep',
9: 'oct',
10: 'nov',
11: 'dec'
};
return months[monthIndex];
}
// ..........
// Here you already read the object from Firestore and send its properties as props to this component.
return(
Published on {mapMonth(props.createdAt.toDate().getMonth()) + ' '}
of {props.createdAt.toDate().getFullYear()}
);
}
Basically when you do createdAt.toDate() you get a JS Date object.
I use it like this all the time!
From: https://cloud.google.com/firestore/docs/manage-data/add-data
This will create a data based on the user's system date. If you need to be sure that everything will be stored chronologically (without any errors of wrong system dates set by your users system) on your DB, you should use a server timestamp. The date will be set using your Firestore DB internal date system.