JSP——时钟、倒计时

JSP——时钟、倒计时_第1张图片


<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>时钟title>
	<script type="text/javascript">
		window.onload = function(){
			var oDiv = document.getElementById('div1');

			function timego(){			
				var now = new Date();
				var year = now.getFullYear();
				var month = now.getMonth()+1;
				var date = now.getDate();
				var week = now.getDay();
				var hour = now.getHours();
				var minute = now.getMinutes();
				var second = now.getSeconds();
				oDiv.innerHTML = '当前时间是:'+year+'年'+todou(month)+'月'+todou(date)+'日'+' '+toweek(week)+'  '+ todou(hour) +':'+ todou(minute)+':'+todou(second);
			}

			timego();


			setInterval(timego,1000);

		}


		function toweek(num){

			switch(num){
				case 0:
					return '星期天';
					break;
				case 1:
					return '星期一';
					break;
				case 2:
					return '星期二';
					break;
				case 3:
					return '星期三';
					break;
				case 4:
					return '星期四';
					break;
				case 5:
					return '星期五';
					break;
				case 6:
					return '星期六';
					break;
			}
		}

		function todou(num){

			if(num<10){
				return '0'+num;				
			}
			else
			{
				return num;
			}

		}







	script>

head>
<body>
	<div id="div1">div>
body>
html>

JSP——时钟、倒计时_第2张图片


<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>时钟title>
	<script type="text/javascript">
		
		// var now = '后台打印时间';


	script>

	<script type="text/javascript">
		window.onload = function(){
			
			var oDiv = document.getElementById('div1');

		
			function timeleft(){
				
				// 实际开发中此时间从服务器获取:
				var now = new Date();


				var future = new Date(2016,11,6,14,53,0);
				var lefts = parseInt((future-now)/1000);

				var day = parseInt(lefts/86400);

				var hour = parseInt((lefts%86400)/3600);

				var minute = parseInt(((lefts%86400)%3600)/60);

				var second = lefts%60;

				if(lefts<=0)
				{
					window.location.href = "http://www.itcast.com";
				}

				oDiv.innerHTML = '距离2016年12月10日晚24时还有'+day+'天'+todou(hour)+'时'+todou(minute)+'分'+todou(second)+'秒';

			}

			timeleft();

			setInterval(timeleft,1000);


		}

		function todou(num){

			if(num<10){
				return '0'+num;				
			}
			else
			{
				return num;
			}

		}

	script>

head>
<body>
	<div id="div1">div>
body>
html>

你可能感兴趣的:(前端)