我试图显示两条消息: 1.活动前一天 2.活动当天 事件发生后的日期消息正常。 我试图添加"距离= 1"这似乎是问题...
var countDownDate = new Date("<? echo $mounthname; ?> <? echo $d[2]; ?>, <? echo $d[0]; ?> <? echo $hourresp; ?>:00").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
document.getElementById("countdown").innerHTML = " <? echo $line_within; ?> " + days + " <? echo $line_days; ?> ";
if (distance === 1) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "<? echo $line_eventistomorrow; ?>";
}
if (distance === 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "<? echo $line_eventistoday; ?>";
}
if (distance < 0) {
clearInterval(x);
document.getElementById("countdown").innerHTML = "<? echo $line_eventisover; ?>";
}
}, 400);
答案 0 :(得分:1)
当您要检查distance
时,您需要检查days
。
(另请注意Math.floor
会在这里给你一个一个错误;你想要Math.ceil
。而且这种数学确实不会给你读数&# 34;明天&#34;以及&#34;昨天&#34;而是你从现在开始24小时后#34;以及&#34; 24小时前&#34;。用于真实的日期比较我建议像moment.js那样简化了大部分内容。)
var countDownDate = new Date("December 1, 2017").getTime();
var now = new Date().getTime();
var distance = countDownDate - now;
console.log(distance); // this will not be 1 unless the countdown date is one millisecond in the future
var days = Math.ceil(distance / (1000 * 60 * 60 * 24));
console.log(days); // this is what you want
if (days === 1) {
// ...
} // etc
&#13;