反应本机解析Firestore时间戳 [英] React Native Parse Firestore Timestamp
问题描述
我意识到绝对有0个关于如何在React中解析Firebase/firestore时间戳的文档.
i realize there is absolutely 0 documentation on how to parse a firebase/firestore timestamp in react.
如此处所示 https://firebase.google.com/docs/reference/android /com/google/firebase/Timestamp
firebase中的时间戳由秒和毫秒组成.
Timestamp in firebase is composed of seconds and miliseconds.
var newobj={
uid_to:'K365J32fddel3QTxGG94VksXtQP2',
uid_from:'RqVngIRyiJV2XTogLSONZIqoe5h1',
monto:23,
fecha:new Date(),
user:{
from_name:"fabri"
},
status:'pending'
}
firestore.collection('transacciones').add(newobj);
}
然后在控制台中显示为
当我进行查询时,它会带来
And when i do a query it brings this
Object {
"fecha": Timestamp {
"nanoseconds": 960000000,
"seconds": 1526130923,
},
"monto": 23,
"status": "pending",
"uid_from": "RqVngIRyiJV2XTogLSONZIqoe5h1",
"uid_to": "K365J32fddel3QTxGG94VksXtQP2",
"user": Object {
"from_name": "fabri",
},
}
我如何将其解析为简单的日期或再次解析为日期时间?
How do i parse it into a simple date, or datetime again?
推荐答案
我也为此而苦苦挣扎,因为我在SO或其他任何地方都找不到答案,所以这是我通过反复试验得出的解决方案.为了完整起见,这里是我将时间戳字段保存到articles集合的方法:
I struggled with this too and since I found no answers on SO or anywhere else, here's the solution that I got through trial and error. For sake of being complete here is how I saved the timestamp field to the articles collection:
saveArticle() {
firebase.firestore().collection('articles').add({
title: this.state.title,
createdAt: firebase.firestore.FieldValue.serverTimestamp(),
})
}
然后显示您需要调用该对象的toDate()方法的日期,将其转换为JavaScript Date对象,然后将其转换为日期字符串.所以在渲染中,我有以下内容.希望这可以节省一些时间.
Then to display the date you need to call the object's toDate() method, convert that to a JavaScript Date object, then convert that to a date string. So in the render I have the below. Hope this saves someone some time.
<Text>{new Date(this.state.article.createdAt.toDate()).toDateString()}</Text>
这篇关于反应本机解析Firestore时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!