首页 > 代码库 > js实现小时钟,js中Date对象的使用?

js实现小时钟,js中Date对象的使用?

介绍一下js中Date对象的使用

dateObj = new Date()

dateObj = new Date(dateValue)

dateObj = new Date(year,month,date[,hours[,minutes[,seconds[,ms]]]]);


以下有一个小样例

	<script type="text/javascript">
		function setTime(){
			//获得如今的时间
			var now = new Date();
	  		var year = now.getFullYear();
	  		var month = now.getMonth();
	  		month ++;
	  		var date = now.getDate();
	  		var day = now.getDay();
	  		var hours = now.getHours();
	  		var minute = now.getMinutes();
	  		var second = now.getSeconds();
			var arr_week = new Array("星期一","星期二","星期三","星期四","星期五","星期六","星期日");
			var week = arr_week[day];
	  		var time = year + "年" + month + "月" + date + "日 " + week + " " + hours + ":" +minute + ":" + second;
			var clock = document.getElementById("clock");
			clock.innerHTML ="当前时间: "+time;
		}
		//下载完毕后,每隔一秒更新一次时间
		window.onload = function(){
			window.setInterval("setTime()", 1000);
		}
  	</script>
显示效果:

技术分享

js实现小时钟,js中Date对象的使用?