I am new in React so I need a help.
So my timer works. But I need a countdown from current date until a certain date (in my situation till 15.07.2022 at 18:00) so I need days, hours, minutes and second till this event. For now I only manually added the numbers. How to implement that, maybe to use Date()? Here is my code:
const Timer = () => {
const [minutes, setMinutes] = useState(30);
const [seconds, setSeconds] = useState(60);
useEffect(() => {
let myInterval = setInterval(() => {
if (seconds > 0) {
setSeconds(seconds - 1);
}
if (seconds === 0) {
if (minutes === 0) {
clearInterval(myInterval);
} else {
setMinutes(minutes - 1);
setSeconds(59);
}
}
}, 1000);
return () => {
clearInterval(myInterval);
};
});
return (
<Box
>
<Box
>
<Box display="flex" flexDirection={'column'} alignItems={'center'}>
<Typography
variant={'h3'}
>
13
</Typography>
<Typography
>
Days
</Typography>
</Box>
<Box display="flex" flexDirection={'column'} alignItems={'center'}>
<Typography
variant={'h3'}
>
09
</Typography>
<Typography
>
Hours
</Typography>
</Box>
<Box display="flex" flexDirection={'column'} alignItems={'center'}>
<Typography
variant={'h3'}
>
{minutes}
</Typography>
<Typography
>
Minute
</Typography>
</Box>
<Box display="flex" flexDirection={'column'} alignItems={'center'}>
<Typography
variant={'h3'}
>
{seconds}
</Typography>
<Typography
>
Seconds
</Typography>
</Box>
</Box>
</Box>
);
};
Thank you all :) I really appreciated your help :)