⽇期格式之——剩余时间(天,⼩时,分,秒)的计算⽅法demo的css样式如下:
* { margin: 0;padding: 0;}
#timer {
font-size: 0px;color: red;font-weight: bolder;
/*对字体进⾏抗锯齿渲染可以使字体看起来会更清晰舒服它有三个属性值:
none: 对低像素的⽂本⽐较好;subpixel-antialiased:默认值;antialiased:抗锯齿很好 */
-webkit-font-smoothing: antialiased;
}
span {font-size: 12px;}
/*将span定宽,将重绘最⼩化*/
#days,#hours,#minutes,#seconds {
display: inline-block;
width: 14px;
text-align: center;
}
demo的html如下:
<div id="timer">
<span id="days"></span><span>天</span>
<span id="hours"></span><span>⼩时</span>font weight bolder
<span id="minutes"></span><span>分</span>
<span id="seconds"></span><span>秒</span>
</div>
demo的js如下:
var leftTime;
function leftTimer(year,month,day,hour,minute,second){
leftTime = (new Date(year,month-1,day,hour,minute,second)) - (new Date()); //计算剩余的毫秒数
var days = parseInt(leftTime / 1000 / 60 / 60 / 24 , 10); //计算剩余的天数
var hours = parseInt(leftTime / 1000 / 60 / 60 % 24 , 10); //计算剩余的⼩时
var minutes = parseInt(leftTime / 1000 / 60 % 60, 10);//计算剩余的分钟
var seconds = parseInt(leftTime / 1000 % 60, 10);//计算剩余的秒数
return {
days: checkTime(days),
hours: checkTime(hours),
minutes: checkTime(minutes),
seconds: checkTime(seconds)
}
}
var timer = setInterval(function(){
var t = leftTimer(2017,11,21,12,0,0);
if (!leftTime) clearInterval(timer);
},1000);
function checkTime(i){ //将0-9的数字前⾯加上0,例1变为01
if(i<10) i = "0" + i;
return i;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。