..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 | // /hooks/useCountdown.ts
import { useState, useEffect } from 'react';
export const useCountdown = (targetDate: number | null): string => {
const [timeLeft, setTimeLeft] = useState('');
useEffect(() => {
if (!targetDate) {
return; // Early exit if targetDate is null or undefined
}
const timer = setInterval(() => {
const now = Date.now();
const distance = targetDate - now;
if (distance < 0) {
clearInterval(timer);
setTimeLeft('Airing now or aired');
return;
}
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60),
);
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
setTimeLeft(
`${days} days, ${hours} hours, ${minutes} minutes, ${seconds} seconds`,
);
}, 1000);
return () => clearInterval(timer);
}, [targetDate]);
return timeLeft;
};
|
|