[英]React Native map function to get date drom database
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.我想在我的前端显示我的数据库(firestore)中的日期,但当它在数据库中明确正确时,它只给我秒和纳秒作为值。
I am doing a map function to get the data from the database.我正在执行 map function 从数据库中获取数据。
<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.上面的代码不起作用,因为它是一个 object 并且需要扩展,但我只能将它扩展到秒和纳秒。
Here is where I console.log the data这是我 console.log 数据的地方
Here is the data in firestore这是 firestore 中的数据
Create a function to convert seconds to date.创建一个 function 以将秒数转换为日期。
const convertToDate = (seconds) => {
return new Date(seconds).toString();
}
Then use it in your jsx.然后在你的 jsx 中使用它。
<Text style={styles.datecon}>{convertToDate(comp.date.seconds)}</Text>
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多亏了答案(来自 bgcodes),我修复了错误并修复了不说 1970 的日期,你应该只用 *1000 来获得正确的日期
const convertToDate = (seconds) => {
return new Date(seconds*1000).toString();
}
<Text style={styles.datecon}>{convertToDate(comp.date.seconds)}</Text>
What you get from the SDK is a Firestore-specific type called a Timestamp
, which has helpful methods listed here .您从 SDK 得到的是一个名为
Timestamp
的特定于 Firestore 的类型,它在此处列出了有用的方法。
One of these is to get a Date
object from the timestamp, which you'd do with, much simpler with:其中之一是从时间戳中获取
Date
object,您可以使用它,使用它更简单:
<Text style={styles.datecon}>{comp.date.toDate()}</Text>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.