js获取当前时间以及30s后的时间

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<div id="Date"></div>
		<div id="30Date"></div>
	</body>

</html>
<script type="text/javascript">
	window.onload = function() {
		setInterval(function() {
			var date = new Date();
			var year = date.getFullYear(); //获取当前年份
			var mon = date.getMonth() + 1; //获取当前月份
			var da = date.getDate(); //获取当前日
			var h = date.getHours(); //获取小时
			var m = date.getMinutes(); //获取分钟
			var s = date.getSeconds(); //获取秒
			var ts=s+30;
			//将时间格式转化为时间戳
			var oDate1 = new Date(year,mon,da,h ,m,s);  //当前的时间
			var oDate2 = new Date(year,mon,da,h,m,ts);  //30s之后的时间
			var nTime = oDate2.getTime() - oDate1.getTime();  //之间的时间戳
		    var nn=timestampToTime(parseInt(nTime+new Date().getTime()));//之间的时间戳+当前的时间
			
			var d = document.getElementById('Date');
			var threed= document.getElementById('30Date');
			d.innerHTML = '当前时间:' + year + '年' + mon + '月' + da + '日  ' + h + ':' + m + ':' + s;
			threed.innerHTML = '当前30s之后的时间:' +nn;
			tt();
		}, 30)
		
		function timestampToTime(nTime) {
		        var date = new Date(nTime);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
		        var Y = date.getFullYear() + '-';
		        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
		        var D = date.getDate() + ' ';
		        var h = date.getHours() + ':';
		        var m = date.getMinutes() + ':';
		        var s = date.getSeconds();
		        return Y+M+D+h+m+s;
		}
		
		function tt(){
			console.log('fff');
		}
		
		
	}




</script>

猜你喜欢

转载自blog.csdn.net/qq_37481512/article/details/82144032