2017-03-31 159 views
0

我目前有一个倒数计时器,每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); 

回答

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

你忘了加空格。

0

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

之间加空格只需使用例如:

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