倒数计时器,不显示时间

时间:2017-03-31 16:10:46

标签: javascript date timer

我目前有一个倒计时器,每24小时重置一次,但有些人现在显示“NaN”而不是时间。我觉得它可能与变量有关吗?

有人可以帮我纠正问题所在吗?

// 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);

2 个答案:

答案 0 :(得分:1)

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

您忘了添加空格。

答案 1 :(得分:0)

您需要在monthNames[month]day"2017 24:00:00"

之间添加空格

只需使用该示例:

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