【发布时间】:2020-05-26 06:41:33
【问题描述】:
使用 moment 格式化从 firestore 时间戳检索的日期。但是,日期至少相差一天,最多几个月。无论如何,这一年都会减少 50 岁。
这是 Firestore 时间戳
编辑:这是从lastMsg.seconds 记录的内容:
1581372232
我在 FlatList 的 renderItem 中检索以秒为单位的时间:
renderItem={({ item, index }) => {
return (
<Components.InboxItem
title={item.withName}
subtitle={item.lastMsg.seconds}
img={item.withImg}
/>
);
最后在组件内部我使用 moment 像这样:
const date = moment()
.utc()
.startOf('year')
.seconds(props.subtitle)
.format('MMMM DD YYYY');
虽然我尝试了多种格式配置,但最接近准确的一种是.startOf("year")。即便如此,日期仍显示为“2070 年 2 月 9 日”。如果.startOf() 更改为“月”、“日”或“小时”,则日期将更改为三月的某个时间。如何解决此问题以在 firestore 中显示日期?
【问题讨论】:
-
您能否在控制台时间戳的屏幕截图旁边添加您从 firestore 在 js 代码中检索到的实际数据?
-
编辑时间以秒为单位
-
问题是
moment().utc().startOf('year')给你 2020/01/01,而.seconds(...)添加等于日期。 unix 纪元 (1970/01/01) 和 2020 年之间的年数为 50,因此您将在 2020 年增加约 50 年。
标签: firebase react-native google-cloud-firestore momentjs