网页倒计时,动态显示"××年还剩××天××时××分××秒"

var target = document.getElementById('target');
function getTimeString(){
	var date = new Date();
	var eachMonthDay = [31,28,31,30,31,30,31,31,30,31,30,31];
	if(date.getFullYear() % 4 == 0){
		eachmonthDay[1] = 29;
	}
	var dateString = date.getFullYear() + "年还剩" + (11-date.getMonth()) + "月";
	//计算还剩多少天,以当前月天数为基准
	dateString += eachMonthDay[date.getMonth()] - date.getDate() + "天";
	//计算还剩多少小时,以当天为基准
	dateString += (24 - date.getHours() - 1) + "小时";
	//计算还剩多少分钟,以当前时间为准
	dateString += (60 - date.getMinutes() -1) + "分";
	//计算还剩多少秒,以当前时间为准
	dateString += (60 - date.getSeconds()) + "秒";
	target.innerHTML = dateString;
}
var timer = setInterval(getTimeString,1000);

这种做法开销比较大,应该还有更好的算法

你可能感兴趣的:(网页倒计时,动态显示"××年还剩××天××时××分××秒")