import { useEffect, useState } from 'react' const CountDown = ({ initialTime }) => { const days = Math.floor(initialTime / 86400) const hours = Math.floor((initialTime % 86400) / 3600) const minutes = Math.floor((initialTime % 3600) / 60) const seconds = initialTime % 60 const [timeLeft, setTimeLeft] = useState({ day: days, hour: hours, minute: minutes, second: seconds }) useEffect(() => { const timer = setInterval(() => { const totalSeconds = timeLeft.day * 86400 + timeLeft.hour * 3600 + timeLeft.minute * 60 + timeLeft.second const secondsLeft = totalSeconds - 1 if (secondsLeft < 0) { clearInterval(timer) } else { const days = Math.floor(secondsLeft / 86400) const hours = Math.floor((secondsLeft % 86400) / 3600) const minutes = Math.floor((secondsLeft % 3600) / 60) const seconds = secondsLeft % 60 setTimeLeft({ day: days, hour: hours, minute: minutes, second: seconds }) } }, 1000) return () => clearInterval(timer) }, [timeLeft]) return (
{timeLeft.day.toString().padStart(2, '0')} Hari
{timeLeft.hour.toString().padStart(2, '0')} Jam
{timeLeft.minute.toString().padStart(2, '0')} Menit
{timeLeft.second.toString().padStart(2, '0')} Detik
) } export default CountDown