Last active
March 10, 2020 08:51
-
-
Save toxi-kb/3f4cc084160ba9e4b4b102dae1146270 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const convertTimeDiff = diff => ({ | |
| dd: Math.floor(diff / (1000 * 60 * 60 * 24)), | |
| hh: Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)), | |
| mm: Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)), | |
| ss: Math.floor((diff % (1000 * 60)) / 1000) | |
| }); | |
| const useTimer = timestamp => { | |
| const [timer, setTimer] = useState(null); | |
| useEffect(() => { | |
| const intervalId = setInterval(() => { | |
| setTimer(convertTimeDiff(timestamp - Date.now())); | |
| }, 1000); | |
| setTimer(convertTimeDiff(timestamp - Date.now())); | |
| return () => clearInterval(intervalId); | |
| }, [timestamp]); | |
| return timer; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment