以下是JS日期倒计时代码示例:

<!DOCTYPE html>
<html>
<head>
	<title>JS Countdown Timer</title>
</head>
<body>
	<h1 id="countdown"></h1>

	<script>
		// Set the date we're counting down to
		var countDownDate = new Date("Jan 1, 2022 00:00:00").getTime();

		// Update the countdown every 1 second
		var x = setInterval(function() {

			// Get today's date and time
			var now = new Date().getTime();

			// Find the distance between now and the countdown date
			var distance = countDownDate - now;

			// Time calculations for days, hours, minutes and seconds
			var days = Math.floor(distance / (1000 * 60 * 60 * 24));
			var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
			var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
			var seconds = Math.floor((distance % (1000 * 60)) / 1000);

			// Display the countdown
			document.getElementById("countdown").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";

			// If the countdown is finished, display a message
			if (distance < 0) {
				clearInterval(x);
				document.getElementById("countdown").innerHTML = "EXPIRED";
			}
		}, 1000);
	</script>
</body>
</html>

以上代码将在页面上显示一个倒计时,直到指定日期。该代码使用setInterval()方法每隔1秒更新一次倒计时,并使用JavaScript进行日期和时间计算。innerHTML属性用于将计算结果显示在页面上。

JS日期倒计时代码

原文地址: https://www.cveoy.top/t/topic/bnGM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录