
[img]https:/ /i.sstatic.net/V0Ivuoqt.png[/img]
Поэтому изначально у меня есть простой обратный отсчет:
Код: Выделить всё
const targetDate = new Date('2024-09-01T07:00:00');
const [timeLeft, setTimeLeft] = useState({
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
});
useEffect(() => {
const updateCountdown = () => {
const currentTime = new Date();
const difference = targetDate - currentTime;
if (difference < 0) {
clearInterval(intervalId);
setTimeLeft({ days: 0, hours: 0, minutes: 0, seconds: 0 });
} else {
const days = Math.floor(difference / (1000 * 60 * 60 * 24));
const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((difference % (1000 * 60)) / 1000);
setTimeLeft({ days, hours, minutes, seconds });
}
};
const intervalId = setInterval(updateCountdown, 1000);
return () => clearInterval(intervalId);
}, [targetDate]);
Код: Выделить всё
{String(timeLeft.days).padStart(3, '0')}
days
{String(timeLeft.hours).padStart(2, '0')}
hours
{String(timeLeft.minutes).padStart(2, '0')}
min
{String(timeLeft.seconds).padStart(2, '0')}
sec
Код: Выделить всё
String(timeLeft.seconds).padStart(3, '0')
Есть идеи, как это исправить?
Подробнее здесь: https://stackoverflow.com/questions/784 ... displaying