user7582130
user7582130

Reputation:

Countdown timer, not displaying time

I currently have a countdown timer that resets every 24 hours but for some now it displays "NaN" instead of the time. I have a feeling it may be to do with the variables ?

Can someone please help me rectify what the issue may be ?

// Set the date we're counting down to
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var today = new Date();
var day = today.getDate();
var month = today.getMonth() ;
var year = today.getFullYear();
//alert(year); You can check the year from here
var countDownDate = new Date( monthNames[month] + day + "2017 24:00:00").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

// Get todays date and time
var now = new Date().getTime();

// Find the distance between now an 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);

if(hours < 10){
var hours = "0" + hours;

}
if(minutes<10){
var minutes = "0" + minutes;

}
if(seconds <10){
var seconds = "0" + seconds;
}

// Output the result in an element with id="demo"
document.getElementById("H_one").innerHTML = hours ;
document.getElementById("M_one").innerHTML = minutes;
document.getElementById("s_one").innerHTML = seconds ;

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);

Upvotes: 0

Views: 75

Answers (2)

Pasha.Proton
Pasha.Proton

Reputation: 71

You need add spaces between monthNames[month], day and "2017 24:00:00"

Just use that example:

var countDownDate = new Date( monthNames[month] + " " + day + " 2017 24:00:00").getTime();

Upvotes: 0

Ronan
Ronan

Reputation: 1512

var countDownDate = new Date( monthNames[month] + " " + day + " " + "2017 24:00:00").getTime();

You forgot to add spaces.

Upvotes: 1

Related Questions