获取本机时间,再HTML中显示

匿名 (未验证) 提交于 2019-12-02 20:32:16
<!DOCTYPE html> <html lang="en"> <head> 	<meta charset="UTF-8"> 	<title>Document</title> 	<style type="text/css"> 		div{width: 300px;height: 100px;margin: 50px auto;border:1px solid #888;line-height: 100px;text-align: center;} 	</style> </head> <body> 	<div id="box"> 		 	</div> 	<script type="text/javascript"> 		var a=document.getElementById('box');  		function showTime(){  			var date = new Date();//创建日起对象的实例  			//获取年份 			var year = date.getFullYear(); 			var month = date.getMonth()+1; 			var day = date.getDate(); 			if (month<10) { 				month= "0"+month; 			} 			if (day<10) { 				day="0"+day; 			}  			//获取时分秒 			var hour = date.getHours(); 			var minute = date.getMinutes(); 			var second = date.getSeconds(); 			if (hour<10) { 				hour="0"+hour; 			} 			if (minute<10) { 				minute="0"+minute; 			} 			if (second<10) { 				second="0"+second; 			} 			//获取星期 			var week = date.getDay();  			//输出 			var time = year + "年" + month + "月" + day + "日" +"  &nbsp;&nbsp;"+ hour + ":" + minute + ":" + second +"  &nbsp;&nbsp;"+ "星期" + week; 			a.innerHTML=time; 		} 		showTime(); 		setInterval(showTime,1000); 	</script> </body> </html> 
文章来源: https://blog.csdn.net/weixin_44321299/article/details/86021084
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!