js算出此时此刻的时间和距离某一天的时间

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>Swiper demo</title>
		<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">

		<!-- Demo styles -->
		<style>
			div.clock {
				text-align: center;
				font-size: 30px;
			}

			div.clock>span {
				padding: 6px;
				background-color: skyblue;
			}

			.title {
				width: 430px;
				height: 45px;
				margin: 200px auto 50px;
			}

			.title-item strong {
				background-color: orange;
				color: #fff;
				font-size: 36px;
				padding: 0 10px;
				border-radius: 5px;
				box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
				font-family: Arial, Helvetica, sans-serif;
				margin-right: 10px;
				line-height: 49px;
			}

			.title-item {
				width: 430px;
				height: 45px;
				margin: 0 auto;
			}

			.title-item>span {
				line-height: 49px;
				font-size: 32px;
				color: orange;
			}
			.clock span{color:red;}
		</style>
	</head>
	<body>
		 <div class="clock">
			  <span id="d"></span>:
		    <span id="h"></span>:
		    <span id="m"></span>:
		    <span id="s"></span>
		</div>
		<h1 class="title">距离国庆节10月1号,还有</h1>
		<div class="title-item">
		    <span><span id="days"></span>天</span>
		    <strong><span id="hours"></span>时</strong>
		    <strong><span id="minutes"></span>分</strong>
		    <strong><span id="seconds"></span>秒</strong>
		</div>

		<script src="swiper4/jquery.min.js"></script>
		<script>
			function getCurrentTime(){
				var date=new Date();
				var ds=toTwo(date.getDate());
				var hs=toTwo(date.getHours());
				var ms=toTwo(date.getMinutes());
				var ss=toTwo(date.getSeconds());
				$('#d').html(ds);
				$('#h').html(hs);
				$('#m').html(ms);
				$('#s').html(ss);
			}
			function toTwo(val){
				return val<10?'0'+val:val
			}
			getCurrentTime()
			setInterval(getCurrentTime, 1000);
			// 倒计时 距离国庆节10月1日还有多少天多少时多少分多少秒
			var endTime = new Date("2019-10-01 0:0:0");
			setInterval(getDjs,1000);
			function getDjs(){
				var now=new Date();
				var diff=endTime.getTime()-now.getTime();
				diff=diff/1000;
				var day,hour,minute,seconds;
				day=Math.floor(diff/(24*60*60));
				hour=Math.floor((diff/(60*60))%24);
				minute=Math.floor(diff/60%24);
				seconds=Math.floor(diff%60);
				$('#days').html(toTwo(day));
				$('#hours').html(toTwo(hour));
				$('#minutes').html(toTwo(minute));
				$('#seconds').html(toTwo(seconds));
			}
			getDjs();
			
		</script>
	</body>
</html>

posted @ 2019-07-15 11:16  lxystar  阅读(522)  评论(1编辑  收藏  举报