JavaScript内置对象Date

调用JavaScript内置的对象Date,可以得到当前的“年月日周时分秒”,同时将其显示在网页上。

具体代码如下:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<span id="time"></span>
		<script>
			function show(){
				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth()+1;//月份是从0开始计的
				month = month<10?"0"+month:month;
				var day = date.getDate();
				day = day<10?"0"+day:day;
				var week = date.getDay();
				week = "星期"+"日一二三四五六七".charAt(week);
				var hour = date.getHours();
				hour = hour<10?"0"+hour:hour;
				var minute = date.getMinutes();
				minute = minute<10?"0"+minute:minute;
				var second = date.getSeconds();
				second = second<10?"0"+second:second;
				var result = year+"-"+month+"-"+day+" "+week+" "+hour+":"+minute+":"+second;
				document.getElementById("time").innerHTML = result;//向id为time的javascript对象中加入元素
			};
			show();
			setInterval("show()",1000);
		</script>
	</body>
</html>

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页