如何利用JavaScript来获取时间?

		<span id="time"></span>
		<script>
			function show(){
				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth()+1;
				month = month<10?"0"+month:month;
				var day = date.getDate();
				day = day<10?"0"+day:day;
				var week = date.getDay();
					switch(week){
						case 1:
						week="星期一";
						break;
						case 2:
						week="星期二";
						break;
						case 3:
						week="星期三";
						break;
						case 4:
						week="星期四";
						break;
						case 5:
						week="星期五";
						break;
						case 6:
						week="星期六";
						break;
						case 0:
						week="星期日";
						break;
					}
				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;
			}
			show();
			setInterval("show()",1000);

执行结果如下: 

但是以上代码,可以简化:可以将switch语句进行将String语句简化

		<span id="time"></span>
		<script>
			function show(){
				var date = new Date();
				var year = date.getFullYear();
				var month = date.getMonth()+1;
				month = month<10?"0"+month:month;
				var day = date.getDate();
				day = day<10?"0"+day:day;
				var week = date.getDay();
				week="日一二三四五六".charAt(week);
				week="星期"+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;
			}
			show();
			setInterval("show()",1000);
		</script>	

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页