Skip to content

Instantly share code, notes, and snippets.

@MrCordeiro
Created January 23, 2021 16:35
Show Gist options
  • Save MrCordeiro/e3f90ee77dde56aad4e5a6d3266abdbc to your computer and use it in GitHub Desktop.
Save MrCordeiro/e3f90ee77dde56aad4e5a6d3266abdbc to your computer and use it in GitHub Desktop.
timer-progress-bar
/**
* Displays the remaining time before date
*/
timer = function () {
// Set the date we're counting down to
const startDate = new Date(Date.UTC(2020, 9, 5));
const endDate = new Date(Date.UTC(2021, 0, 15));
const now = new Date();
const timeTotal = endDate - startDate;
const timeLeft = endDate - now;
// update countdown
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
document.getElementById("daysLeft").innerHTML = days;
// Update progress bar
const progressBar = document.querySelector(".progress-bar");
const pctFilled = Math.floor(100 * (1 - timeLeft/timeTotal)) ;
progressBar.setAttribute("aria-valuenow", pctFilled);
progressBar.style.width = pctFilled + "%";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment