简体   繁体   English

React Native map function 获取日期drom数据库

[英]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 数据的地方

Firestore

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.

相关问题 日期 { NaN } React Native - Date { NaN } React Native React Native Firebase RT 数据库错误 - “Undefined is not a function” - React Native Firebase RT Database Error - "Undefined is not a function" 异步 function 本机反应 - Async function react native firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app? React Native实时数据库构建错误 - React Native realtime database build error 无法从 React-Native-Firebase(v6) Firestore 获取数据:undefined 不是函数(靠近“...this._firestore.native.collectionGet...”) - Can't get data from React-Native-Firebase(v6) Firestore: undefined is not a function (near '...this._firestore.native.collectionGet...') 如何在 React Native 中使用 Firestore 数据库进行用户过滤? - How to user filter with Firestore database in React Native? 加载 Map 视图时 React Native Firebase 抛出错误 - React Native Firebase Throwing Error when Map view is loaded 如何根据 React Native 中的英里设置 google map 中的缩放级别? - How to set zoom level in google map according to miles in react native? .map function 未在 React JS 中呈现所有数据 - .map function is not rendering all data in react js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM