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 }//
dates is an array of UNIX timestamps [1648703685000, 1648623600000] which you are trying to pass to the Date() constructor. But Date() 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.
const dates = [1648703685000, 1648623600000]
const parsedDates = dates.map(date => new Date(date));
const [startD, endD] = parsedDates;
console.log("Start:", startD);
console.log("End:", endD);
BTW: You can do the above in one line if you want to:
const [startD, endD] = dates.map(date => new Date(date));