I would like to display the date form my database (firestore) on my frontend but it only gives me seconds and nanoseconds as values when it clearly correct in the database.
I am doing a map function to get the data from the database.
<Text style={styles.datecon}>{comp.date.seconds}</Text>
The above code works but gives me a string of all the seconds.
<Text style={styles.datecon}>{comp.date}</Text>
The above code doesn't work due to it being an object and needs to be expanded but I can onlyu expand it to seconds and nanoseconds.
Here is where I console.log the data
Here is the data in firestore
CodePudding user response:
Create a function to convert seconds to date.
const convertToDate = (seconds) => {
return new Date(seconds).toString();
}
Then use it in your jsx.
<Text style={styles.datecon}>{convertToDate(comp.date.seconds)}</Text>
CodePudding user response:
Thanks to the answer (from bgcodes) I mahnage to fix the error and also fixing the date to not say 1970 you should just *1000 to get the correct date
const convertToDate = (seconds) => {
return new Date(seconds*1000).toString();
}
<Text style={styles.datecon}>{convertToDate(comp.date.seconds)}</Text>
CodePudding user response:
What you get from the SDK is a Firestore-specific type called a Timestamp
, which has helpful methods listed here.
One of these is to get a Date
object from the timestamp, which you'd do with , much simpler with:
<Text style={styles.datecon}>{comp.date.toDate()}</Text>