日期{nan}反应本地
我正在使用 React Native,我正在尝试将时间戳数据转换为日期,但输出显示“Date { NaN }
const [eventData1, setEventData1] = useState({});
useEffect(() => {
database()
.ref('path')
.on('value', snapshot => {
if (snapshot.val() !== null) {
setEventData1({...snapshot.val()});
} else {
setEventData1({});
}
});
return () => {
setEventData1([]);
};
}, []);
const dates = Object.keys(eventData1).map(
id => eventData1[id].start_DATE * 1000,
);
const sDate = new Date(dates);
”
console.log(dates) -----> //[1648703685000, 1648623600000]//
console.log(sDate) ------> //Date { NaN }//
I'm using react native and I'm trying to convert my timestamp data to date but the output said "Date { NaN }"
const [eventData1, setEventData1] = useState({});
useEffect(() => {
database()
.ref('path')
.on('value', snapshot => {
if (snapshot.val() !== null) {
setEventData1({...snapshot.val()});
} else {
setEventData1({});
}
});
return () => {
setEventData1([]);
};
}, []);
const dates = Object.keys(eventData1).map(
id => eventData1[id].start_DATE * 1000,
);
const sDate = new Date(dates);
Outputs
console.log(dates) -----> //[1648703685000, 1648623600000]//
console.log(sDate) ------> //Date { NaN }//
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dates
是一个 UNIX 时间戳数组[1648703685000, 1648623600000]
,您尝试将其传递给Date()
构造函数。但是Date()
不接受数组,但可以接受单个时间戳作为参数。您必须循环遍历日期才能解析开始日期和(大概)结束日期。顺便说一句:如果您愿意,您可以在一行中完成上述操作:
dates
is an array of UNIX timestamps[1648703685000, 1648623600000]
which you are trying to pass to theDate()
constructor. ButDate()
does not take an array but can take a single timestamp as a parameter. You will have to loop over the dates to get the start and (presumably) end date parsed.BTW: You can do the above in one line if you want to: