使用矩来格式化从防火墙时间戳中检索的日期。然而,这个日期至少要推迟一天,最多几个月。不管发生什么事,今年都将结束50天。
这是消防局的时间戳

编辑:下面是从lastMsg.seconds:1581372232记录的内容
我在FlatList的renderItem中以秒为单位检索时间:
renderItem={({ item, index }) => {
return (
<Components.InboxItem
title={item.withName}
subtitle={item.lastMsg.seconds}
img={item.withImg}
/>
);最后,在组件内部,我使用了这样的矩:
const date = moment()
.utc()
.startOf('year')
.seconds(props.subtitle)
.format('MMMM DD YYYY');虽然我尝试了多种格式的配置,但最接近精确的配置是使用.startOf("year")。即使如此,日期仍显示为“2070年2月09日”。如果将.startOf()更改为“月”、“日”或“小时”,则日期将改为三月的某个时候。如何将其固定以显示日期,就像在消防局中一样?
发布于 2020-02-10 22:18:41
查看https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp,我们可以获得JS Date对象,也可以使用toMillis方法获得毫秒。
现在给出了将时间戳转换为矩对象的简单moment.js api,https://momentjs.com/docs/#/parsing/unix-timestamp-milliseconds/
moment(Number);
现在,您可以在矩对象上应用格式,如下所示:
moment(Number).format(String);
您的错误日期可能是由于将utc和seconds一起使用而不是将时间戳传递给moment()造成的。
发布于 2020-02-10 22:19:07
const props = {
subtitle: 1581372232
};
const date = moment
.unix(props.subtitle)
.format('MMMM DD YYYY');
console.log(date);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
自Unix纪元1970-01-01T00:00:00Z
以来的协调时秒数
https://stackoverflow.com/questions/60158997
复制相似问题