Can't assign the end date to the variable for the timer to work. As a result, I get NaN
Can you please tell me what is the mistake?
var countDownDate = new Date($("countdown").data("datetime")).getTime();
function ctd() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
countdown = document.getElementsByClassName("countdown");
countdown[0].innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
countdown.innerHTML = "Item expired!";
}
}
ctd();
var x = setInterval(ctd, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<time class="countdown" datetime="2020-11-22T16:20:22+00:00"></time>
You repeat some of the time calculations every second, better use some const instead:
const eCountDown = document.querySelector('.countdown')
, one_Sec = 1000
, one_Min = one_Sec * 60
, one_Hour = one_Min * 60
, one_Day = one_Hour * 24
;
var countDownDate = new Date(eCountDown.dateTime).getTime()
;
function ctd()
{
let now = new Date().getTime()
, distance = countDownDate - now
, days = Math.floor(distance / one_Day)
, hours = Math.floor((distance % one_Day) / one_Hour)
, minutes = Math.floor((distance % one_Hour) / one_Min)
, seconds = Math.floor((distance % one_Min) / one_Sec)
;
eCountDown.textContent = days + 'd '
+ hours + 'h '
+ minutes + 'm '
+ seconds + 's '
;
if (distance < 0)
{
clearInterval(timerIntv)
countdown.textContent = 'Item expired!'
}
}
ctd()
var timerIntv = setInterval(ctd, 1000)
<time class="countdown" datetime="2020-11-22T16:20:22+00:00"></time>
You need to use .countdown to get a class. You were missing the dot (.)
Also you should use attr or prop and not data. You can use data if you change your html property to data-datetime.
When in doubt, always console.log(countDownDate) to see what you get first.
var countDownDate = new Date($(".countdown").attr("datetime"));
function ctd() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
countdown = document.getElementsByClassName("countdown");
countdown[0].innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
countdown.innerHTML = "Item expired!";
}
}
ctd();
var x = setInterval(ctd, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<time class="countdown" datetime="2020-11-22T16:20:22+00:00"></time>
Related
looking for help with a countdown timer I have to reveal a container the page at a specific time. I'd like the "days" "hours" "minutes" to disappear when they reach zero.
Can anyone help?
<h3 id="demo"></h3>
<script>
// Set the date we're counting down to
var countDownDate = new Date("1/21/2022 11:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// 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 + " day " + hours + " hours " + minutes + " min " + seconds + " sec ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "Reserve Tickets";
}
}, 1000);
</script>
Update variables only if distance > 0
<h3 id="demo"></h3>
<script>
// Set the date we're counting down to
var countDownDate = new Date("1/21/2022 11:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
let now = new Date().getTime();
// Find the distance between now and the count down date
let distance = countDownDate - now;
// If the count down is over, write some text
if (distance >= 0) {
// Time calculations for days, hours, minutes and seconds
let days = Math.floor(distance / (1000 * 60 * 60 * 24));
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);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + " day " + hours + " hours " + minutes + " min " + seconds + " sec ";
} else {
clearInterval(x);
document.getElementById("demo").innerHTML = "Reserve Tickets";
}
}, 1000);
</script>
var start = new Date;
start.setHours(14, 0, 0); //2pm
var x = setInterval(function() {
var now = new Date().getTime();
var distance = start - now;
//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);
document.getElementById("countdown").innerHTML = "Order before " + /*days + "d " +*/ hours + "h "
+ minutes + "m " + seconds + "s " + " for delivery tomorrow";
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "EXPIRED";
}
}, 1000);
At the moment i have this JS which after 2pm it says 'Expired' but i no longer want that and wasnt sure how to just reset the timer and make it start counting towards tomorrows 2pm
var d = new Date();
var local_date = new Date( Date.UTC( d.getFullYear(), d.getMonth(), d.getDate(), 14,0) );
var now = new Date().getTime();
var distance = local_date - now;
Change your codes as given above.
set your local_date variable with local date and time.
local_date is also a time.
You may also ignore UTC as per your requirements.
I'm currently using a form so that the user can change the date the timer is set to count down to. The problem is that the Javascript function that's supposed to run when the user clicks the "submit" button is working, but even though I've entered something into the "date" input box of the form, multiple tests have shown that it doesn't show that I've input anything. I don't understand what I need to do. Here is my code:
<html>
<p id="demo"></p>
<div class='form'>
<form>
enter any date here <br>
<br>
<input type="date" id="date"> <br>
<br>
<button onclick='countdown()' class="submitbutton">submit</button>
</form>
</div>
<script>
if (document.cookie=="[object HTMLInputElement]") {
var countDownDate = new Date("10/17/2020").getTime();
}
else {
var countDownDate = new Date(document.cookie).getTime();
}
var countDownDate = new Date("10/17/2020").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}, 1000);
</script>
<script>
function countdown() {
var date = document.getElementById("date");
document.cookie = date;
countDownDate = new Date("10/17/2020").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "the date you entered is before today";
}
}, 1000);
}
</script>
</html>
Thank you in advance.
I think that you should enter the count down date. but current code is not working. They have a issues in the part to get the date. I fixed it. please check it.
https://codepen.io/stargroup0075/pen/jOPdRPd
<html>
<p id="demo"></p>
<div class='form'>
<form>
enter any date here <br>
<br>
<input type="date" id="date"> <br>
<br>
<button onclick='countdown()' class="submitbutton" type="button">submit</button>
</form>
</div>
<script>
if (document.cookie=="[object HTMLInputElement]") {
var countDownDate = new Date("10/17/2020").getTime();
}
else {
var countDownDate = new Date(document.cookie).getTime();
}
var countDownDate = new Date("10/17/2020").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}, 1000);
</script>
<script>
function countdown() {
var date = document.getElementById("date").value;
document.cookie = date;
countDownDate = new Date(date).getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "the date you entered is before today";
}
}, 1000);
}
</script>
</html>
I'm trying to convert the 0 in stock text on my website (held within a <p> tag) to a countdown timer, once the stock level hits 0. So I've added this code to the footer - however it seems to just stick, and not count down at all. It also takes a few seconds to replace the 0 in stock text - can I make this quicker/instant? Here's the code so far:
// Set the count down date
var countDownDate = new Date("Feb 21, 2021 15:26:00").getTime();
// Update the count every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// 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);
// Display the result if stock = 0
list = document.getElementsByClassName("stock in-stock");
if (list[0].innerHTML == "0 in stock") {
list[0].innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}
// If the count down is finished, write text
if (distance < 0) {
clearInterval(x);
list = document.getElementsByClassName("stock in-stock");
list[0].innerHTML = "Item expired!";
}
}, 1000);
<p class="stock in-stock">1 in stock</p>
You were trying to populate the P with new data and somehow trying to read out the old data, I've just separated that into 2 spans so you can work with each one individually and updated your JS to reflect the new structure.
To speed up the first call, extract the function:
Please note that we're treating "stock" and "countdown" as 2 different things now.
// Set the count down date
var countDownDate = new Date("Feb 21, 2021 15:26:00").getTime();
function ctd() {
// Get today's date and time
var now = new Date().getTime();
// 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);
//console.log(days + " " + hours + " " + minutes + " " + seconds);
// Display the result if stock = 0
countdown = document.getElementsByClassName("countdown");
stock = document.getElementsByClassName("stock-level");
if (stock[0].innerHTML == "1") {
countdown[0].innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}
// If the count down is finished, write text
if (distance < 0) {
clearInterval(x);
countdown.innerHTML = "Item expired!";
}
}
ctd(); // run now
// Update the count every 1 second
var x = setInterval(ctd, 1000);
<p class="stock-level" style="display:none">1</p>
<p class="countdown"></p>
I got it to work. Here's the code:
<script>
//Set the count down date
var countDownDate = new Date("Feb 20, 2019 18:26:00").getTime();
var startTimer=false;
list = document.getElementsByClassName("stock in-stock");
if(list[0].innerHTML=='1 in stock') {
startTimer=true;
}
//Check if 1 left
list = document.getElementsByClassName("stock in-stock");
//Update the count every 1 second
var x = setInterval(function() {
//Get today's date and time
var now = new Date().getTime();
//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);
//Update the counter
if(startTimer) {
list[0].innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
}
//If the count down is finished, write text
if (distance < 0) {
clearInterval(x);
list = document.getElementsByClassName("stock in-stock");
list[0].innerHTML = "Item expired!";
}
}, 1000);
</script>
I have requirement if countdown timer in my project so I am using code for that but that code showing 24 hrs format but i want 12 hours format with AM or PM should show instead of 13,14,15 I want hours should come like this
1:00 PM,2:00 PM,3:00 PM.any idea how to do this ?
<p id="check_time"></p>
<p id="check_hours"></p>
<script>
var countDownDate = new Date("may 17, 2018 01:37:25").getTime(); //this the which i have to count for difference
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
document.getElementById("check_hours").innerHTML = hours;
document.getElementById("check_time").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
Try the following:
var countDownDate = new Date("june 17, 2018 01:37:25").getTime(); //this the which i have to count for difference
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
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);
var n = new Date();
var h = n.getHours() < 10 ? '0' + n.getHours() : n.getHours();
var min = n.getMinutes() < 10 ? '0' + n.getMinutes() : n.getMinutes();
var sec = h < 10 ? '0' + n.getSeconds() : n.getSeconds();
var time = n.getHours() + ':' + min + ':' + sec;
document.getElementById("check_hours").innerHTML = tConvert(time);
document.getElementById("check_time").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
function tConvert (time) {
// Check correct time format and split into components
time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
if (time.length > 1) { // If time format correct
time = time.slice (1); // Remove full string match value
time[5] = +time[0] < 12 ? ' AM' : ' PM'; // Set AM/PM
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join (''); // return adjusted time or original string
}
<p id="check_time"></p>
<p id="check_hours"></p>
<p id="demo"></p>