**html部分**
<div id="times">
<span id="days">天</span><span id="hours">时</span>
<span id="minutes">分</span>
<span id="seconds">秒</span>
</div>
JS部分:
function counttime(){
var starTime = new Date("2017/6/4 12:22:00");
var nowTime = new Date();
var endTime = starTime - nowTime;
var days = parseInt(endTime / 1000 / 60 / 60 / 24);
var hours = parseInt(endTime / 1000 / 60 / 60 % 24);
var minutes = parseInt(endTime / 1000 / 60 % 60);
var seconds = parseInt(endTime / 1000 % 60);
if (days < 10){
days = "0" + days ;
}
if (hours < 10){
hours = "0" + hours;
}
if (minutes < 10){
minutes = "0" + minutes ;
}
if (seconds < 10){
seconds = "0" + seconds ;
}
if (endTime <= 0){
$("#times").html("活动结束");
}
$("#days").html(days + " :");
$("#hours").html(hours + " :");
$("#minutes").html(minutes + " :");
$("#seconds").html(seconds);
}
setInterval(counttime,1000);