Skip to content

Instantly share code, notes, and snippets.

@abou7mied
Last active March 20, 2020 19:11
Show Gist options
  • Select an option

  • Save abou7mied/730e2ac669e36e7ee9bdff02969e03bc to your computer and use it in GitHub Desktop.

Select an option

Save abou7mied/730e2ac669e36e7ee9bdff02969e03bc to your computer and use it in GitHub Desktop.
const showDuration = startTime => {
let interval;
const formatTime = () => {
const distance = Date.now() - new Date(startTime);
let hours = Math.floor(
(distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((distance % (1000 * 60)) / 1000);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
let d;
if (distance < 0) {
clearInterval(interval);
d = "EXPIRED";
} else {
d = hours + ":" + minutes + ":" + seconds;
}
document.querySelector(".timer").innerHTML = d;
return d;
};
interval = setInterval(formatTime, 1000);
return formatTime();
};
const formatted = showDuration("2020-03-13T23:00:00.000+0000");
console.log("formatted", formatted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment