1. 程式人生 > >js獲取實時時間資訊、星期幾

js獲取實時時間資訊、星期幾

	<span>當前時間:</span>
	<span id="time"></span>
	<span id="xq"></span>

	<script type="text/javascript">

	window.onload=function(){
		//1s載入一次
		setInterval("clock()",1000);
	}
		
	function clock () {
		//全域性變數d,建立Date物件
		d = new Date();
		//獲得日期時間控制元件
		var spt = document.getElementById("time");
		//格式:2018/2/5  19:19:33
		spt.innerHTML=d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日 "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
			
		//獲得星期控制元件
		var spweek = document.getElementById("xq");
		spweek.innerHTML=week();
			
	}

	function week () {
		var weekday = new Array(7)
		weekday[0] = "星期日"
		weekday[1] = "星期一"
		weekday[2] = "星期二"
		weekday[3] = "星期三"
		weekday[4] = "星期四"
		weekday[5] = "星期五"
		weekday[6] = "星期六"
			
		return weekday[d.getDay()];
	}

	</script>

頁面顯示如下: