JSFiddle - React, Tailwind, and code Playground
by grant
HTML
<p id="demo"></p>
<p id="today"></p>
how
set the countdowndate to today at 9am
run it on sun am and wed am
JavaScript
var today = new Date()
var dayN = today.getDate()
var dayOfWeek = today.getDay()
var hour = today.getHours()
var sunAm = today.setHours(9, 0, 0)
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hours: 'numeric' };
document.getElementById("today").innerHTML = sunAm;
//new Date(Date.UTC(year, month, day, hour, minute, second))
var d = new Date(2022, 0, 30, 9, 0, 0)
let human = d.toLocaleString();
console.log(human)
var countDownDate = today.setHours(14, 0, 0)
sunWed(countDownDate)
//console.log(sunAm)
//====================
function sunWed(countDownDate){
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time feb 5 sat
//var now = new Date().setFullYear(2022, 1, 5);
var now = new Date()
//console.log(now)
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
}