//时间戳转日期格式,传入时间戳必须为数字类型
function currentDate(shijianchuo) {
var date = new Date(shijianchuo);
var y = date.getFullYear();
var m = date.getMonth() + 1;
// m = m < 10 ? ('0' + m) : m;
var d = date.getDate();
// d = d < 10 ? ('0' + d) : d;
var h = date.getHours();
// h = h < 10 ? ('0' + h) : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? ('1' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return y + '/' + m + '/' + d + ' ' + h + ':' + minute + ':' + second;
}
倒计时,天:时:分:秒
function countTime() {
var date = new Date();
var now = date.getTime();
//str为日期格式,2018/09/10 09:09:01
var str= currentDate(parseFloat(shijianchuo1))
var endDate = new Date(str);
var end = endDate.getTime();
var leftTime = end-now;
var d,h,m,s;
if (leftTime>=0) {
d = Math.floor(leftTime/1000/60/60/24);
h = Math.floor(leftTime/1000/60/60%24);
m = Math.floor(leftTime/1000/60%60);
s = Math.floor(leftTime/1000%60);
}
d = checkTime(d);
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
function checkTime(i){
if (i<10) {
i = "0"+i;
}
return i;
}
// document.getElementById("_d").innerHTML = d+"天";
document.getElementById("_h").innerHTML = h+"时";
document.getElementById("_m").innerHTML = m+"分";
document.getElementById("_s").innerHTML = s+"秒";
setTimeout(countTime,1000);
}
countTime();