Skip to content

Instantly share code, notes, and snippets.

@toxi-kb
Last active March 10, 2020 08:51
Show Gist options
  • Select an option

  • Save toxi-kb/3f4cc084160ba9e4b4b102dae1146270 to your computer and use it in GitHub Desktop.

Select an option

Save toxi-kb/3f4cc084160ba9e4b4b102dae1146270 to your computer and use it in GitHub Desktop.
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