JavaScript动态显示年月日星期时分秒

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1 id="date_time"></h1>
		<script type="text/javascript">
			function getweek(option){
				// switch(option){
					// case 1:
					// return "周一";
					
					// case 2:
					// return "周二";
					
					// case 3:
					// return "周三";
					
					// case 4:
					// return "周四";
					
					// case 5:
					// return "周五";
					
					// case 6:
					// return "周六";
					
					// case 7:
					// return "周日";
				//}
				var week = "日一二三四五六".charAt(option);
				return "星期"+week;
			}
			
			function showTime(){
				var dateTime=new Date();
				var year = dateTime.getFullYear();
				
				var month= dateTime.getMonth()+1;
				month=month<10?"0"+month:month;
				
				var date=dateTime.getDate();
				date=date<10?"0"+date:date;
				
				var hour=dateTime.getHours();
				hour=hour<10?"0"+hour:hour;
				
				var minute = dateTime.getMinutes();
				minute=minute<10?"0"+minute:minute;
				
				var second  = dateTime.getSeconds();
				second=second<10?"0"+second:second;
				
				var day=dateTime.getDate();
				var week=getweek(dateTime.getDay());
				dateTime=year+"年"+month+"月"+date+"日"+ " " + week+ " " +hour+":"+minute+":"+second;
				console.log(dateTime);
				document.getElementById("date_time").innerHTML=dateTime;
			}
			showTime();
			setInterval("showTime()",1000);
		</script>
	</body>
</html>

  

posted on 2021-06-04 14:16  Fire·  阅读(84)  评论(1编辑  收藏  举报

导航