Hi everyone facing one issue in my react application where I have array of list timestamp's ex:([1651234567, 1651234568, 1651234569]) which I need to convert in to hours minutes & seconds like Ex:([17:46:7, 17:46:8, 17:46:9]) each seconds timer should increase with help of But in my current scenario this logic is not working.
The below code I have defined timestamp state array & also In useEffect I’m calling setInterval time on every 1 second. But below code timer remain constant. Please help on this.
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>
const ConvertTimer = () => {
const [timestamp, setTimestamp] = useState([1651234567, 1651234568, 1651234569]);
const [timeLeft, setTimeLeft] = useState([]);
useEffect(() => {
const timer = setInterval(() => {
const newTimeLeft = timestamp.map((timestamp) => {
const date = new Date(timestamp * 1000);
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return `${hours}:${minutes}:${seconds}`;
});
setTimeLeft(newTimeLeft);
}, 1000);
return () => clearInterval(timer);
}, [timestamp]);
return (
<div>
<h1>Time Left</h1>
<ul>
{timeLeft.map((time) => (
<li key={time}>{time}</li>
))}
</ul>
</div>
)
}