JavaScript中Date()应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hlw521hxq/article/details/82503802
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>时间</title>
	</head>
	<body>
		<span id="time"></span>
		<button id="newtime">获得新时间</button>
		<span id="rs"></span>
	</body>
	<script type="text/javascript">
		var time=document.getElementById("time");
		var newtime=document.getElementById("newtime");
		var rs=document.getElementById("rs")
		var firsttime=new Date();
		time.innerHTML=firsttime;
		var lasttime=null;
		//时间相减计算方式
		newtime.onclick=function(){
			lasttime=new Date();
			// getTime() 方法可返回距 1970 年 1 月 1 日之间的毫秒数
			console.log(lasttime.getTime());
			console.log(firsttime.getTime());
			var o=(lasttime.getTime()-firsttime.getTime())/1000;
			console.log(parseInt(o))
		}
		//js中定义的时间格式:Fri Sep 07 2018 22:19:27 GMT+0800 (中国标准时间)
		console.log("date:"+firsttime);
		//年在计算机中存储是用三位数存储,得到的是后三位
		var year=firsttime.getYear();
		//getfullYear得到是完整的年份数字
		console.log(firsttime.getFullYear())
		console.log(year+"年");
		//月
		console.log(firsttime.getMonth()+"月");
		//日
		console.log(firsttime.getDate()+"日");
		//星期几
		console.log("星期"+firsttime.getDay());
		//时
		var hour=firsttime.getHours();
		console.log(firsttime.getHours()+"时")
		//分
		console.log(firsttime.getMinutes()+"分")
		//毫秒
		console.log(firsttime.getMilliseconds()+"毫秒");
	</script>
</html>

猜你喜欢

转载自blog.csdn.net/hlw521hxq/article/details/82503802