JS 倒计时

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
<div id="timer" style="color: red"></div>
<input type="button" id="tests" value="tests" onclick="fntime()" />
<script type="text/javascript">
window.status="作者:然";
	var maxtime = 60 * 60
	function fntime() {
		var _currentTime = "2013-01-05 11:00:00"
		var _startTime = "2013-01-08 11:00:00"

		var tmpBeginTime = new Date(_currentTime.replace(/-/g, "\/")); //时间转换
		//alert("CurrentTime:" + tmpBeginTime);

		var tmpEndTime = new Date(_startTime.replace(/-/g, "\/")); //时间转换
		//alert("StartTime:" + tmpEndTime);

		//var datFrom = getDateFromString("2007-1-5-05-7");
		//var datTo = getDateFromString("2007-1-6-04-17");
		var _s = (tmpEndTime - tmpBeginTime) / 1000; //得秒
		var _minute = (tmpEndTime - tmpBeginTime) / (1000 * 60); //得分钟
		var _hour = (tmpEndTime - tmpBeginTime) / (1000 * 60 * 60); //得小时
		var days = (tmpEndTime - tmpBeginTime) / (1000 * 60 * 60 * 24); //得天
		//alert(_s);
		maxtime = _s;
		timer = setInterval("CountDown()", 1000);
		/*
		var sumMinTime = 1000 * 60 * 60 * 24 * (365 / 2);
		alert((tmpEndTime - tmpBeginTime) / (1000 * 60 * 60 * 24));
		if ((tmpEndTime - tmpBeginTime) >= sumMinTime) {
			alert("大于");
		} else {
			alert("小于");
		}*/
	}

	//一个小时,按秒计算,自己调整!  
	function CountDown() {
		if (maxtime >= 0) {
			days = Math.floor(maxtime / 60 / 60 / 24);
			hour = Math.floor(maxtime / 60 / 60 % 24);
			minutes = Math.floor(maxtime / 60 % 60);
			seconds = Math.floor(maxtime % 60);
			msg = "距离结束还有" + days + "天" +hour + "时" +minutes + "分" + seconds + "秒";
			document.getElementById("timer").innerHTML = msg;
			if (maxtime == 5 * 60)
				alert('注意,还有5分钟!');
			--maxtime;
		} else {
			clearInterval(timer);
			alert("时间到,结束!");
		}
	}
</script>
</body>
</html>

 

posted @ 2013-01-05 13:51  tokeep  阅读(186)  评论(0编辑  收藏  举报