【发布时间】:2019-09-26 23:39:19
【问题描述】:
我似乎无法让网站正确显示天、小时、分钟和秒的正确信息。我正在尝试显示距离我的下一个活动还有多长时间。
匹配计数器
function matchCounter(){
var launch = new Date('2019', '04', '18', '8', '00');
var days = $('.tg-days');
var hours = $('.tg-hours');
var minutes = $('.tg-minutes');
var seconds = $('.tg-seconds');
setDate();
function setDate(){
var now = new Date();
if( launch < now ){
days.html('<h3>0</h3><h4>Day</h4>');
hours.html('<h3>0</h3><h4>Hour</h4>');
minutes.html('<h3>0</h3><h4>Minute</h4>');
seconds.html('<h3>0</h3><h4>Second</h4>');
}
else{
var s = -now.getTimezoneOffset()*60 + (launch.getTime() - now.getTime())/1000;
var d = Math.floor(s/86400);
days.html('<h3>'+d+'</h3><h4>Day'+(d>1?'s':''),'</h4>');
s -= d*86400;
var h = Math.floor(s/3600);
hours.html('<h3>'+h+'</h3><h4>Hour'+(h>1?'s':''),'</h4>');
s -= h*3600;
var m = Math.floor(s/60);
minutes.html('<h3>'+m+'</h3><h4>Minute'+(m>1?'s':''),'</h4>');
s = Math.floor(s-m*60);
seconds.html('<h3>'+s+'</h3><h4>Second'+(s>1?'s':''),'</h4>');
setTimeout(setDate, 1000);
}
}
}
matchCounter();
我想显示距离 2019 年 5 月 19 日还有多长时间
【问题讨论】:
-
你不应该使用字符串而是整数来初始化
new Date() -
还要确保
setDate()的声明发生在调用它之前。这是一个显示它有效的小提琴:jsfiddle.net/Moonbird_IT/Lq2age09/12
标签: javascript html