时间显示器

这是一个组件页面。
这些内容可以让你的页面用更简洁的方式呈现出更精彩的效果。

请勿擅自进行改动,如有需要可在自己的沙盒中调试

mountain-watcher-v2

这里的所有时间显示器全部显示的时间为中国时区(UTC+8)!!!!!!!

整理作者:summerLI does not match any existing user name

时间显示器请确到秒:

时间显示器请确到天(自动更新):

自动时钟:

倒计时器(自动更新):

使用方式:

  • 注:这里的代码如果要用在wikidot上面请再开头盔和结尾添加:
开头:[[HTML]]
结尾:[[/HTML]]

时间显示器精确到秒

<!DOCTYPE html>
<html>
<head>
    <title>显示当前时间</title>
    <style>
        #datetime {
            font-size: 24px;
            white-space: nowrap;
            text-align: center;
            color: brown;
        }
    </style>
</head>
<body>
    <h1>当前时间:</h1>
    <div id="datetime"></div>
    <script>
        function displayTime() {
            var now = new Date();
            var year = now.getFullYear();
            var month = ('0' + (now.getMonth() + 1)).slice(-2);
            var day = ('0' + now.getDate()).slice(-2);
            var hours = ('0' + now.getHours()).slice(-2);
            var minutes = ('0' + now.getMinutes()).slice(-2);
            var seconds = ('0' + now.getSeconds()).slice(-2);
 
            var dateString = year + "-" + month + "-" + day;
            var timeString = hours + ":" + minutes + ":" + seconds;
 
            // 在页面中显示时间
            document.getElementById("datetime").innerHTML = dateString + " " + timeString;
        }
 
        // 页面加载完成后立即显示时间
        displayTime();
 
        // 每秒钟更新一次时间
        setInterval(displayTime, 1000);
    </script>
</body>
</html>

时间显示器精确到天(自动更新)

<!DOCTYPE html>
<html>
<body>
 
<canvas id="canvas" width="190" height="190"
style="background-color:var(--basalt-main-text-color)">
</canvas>
 
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var radius = canvas.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.90
setInterval(drawClock, 1000);
 
function drawClock() {
  drawFace(ctx, radius);
  drawNumbers(ctx, radius);
  drawTime(ctx, radius);
}
 
function drawFace(ctx, radius) {
  var grad;
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2*Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();
  grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
  grad.addColorStop(0, '#515154');
  grad.addColorStop(0.5, '#AD9EF9');
  grad.addColorStop(1, '#333');
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
  ctx.fillStyle = '#515154';
  ctx.fill();
}
 
function drawNumbers(ctx, radius) {
  var ang;
  var num;
  ctx.font = radius*0.15 + "px arial";
  ctx.textBaseline="middle";
  ctx.textAlign="center";
  for(num = 1; num < 13; num++){
    ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius*0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius*0.85);
    ctx.rotate(-ang);
  }
}
 
function drawTime(ctx, radius){
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    //hour
    hour=hour%12;
    hour=(hour*Math.PI/6)+
    (minute*Math.PI/(6*60))+
    (second*Math.PI/(360*60));
    drawHand(ctx, hour, radius*0.5, radius*0.07);
    //minute
    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
    drawHand(ctx, minute, radius*0.8, radius*0.07);
    // second
    second=(second*Math.PI/30);
    drawHand(ctx, second, radius*0.9, radius*0.02);
}
 
function drawHand(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.lineCap = "round";
    ctx.moveTo(0,0);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos);
}
</script>
 
</body>
</html>

自动时钟:

<!DOCTYPE html>
<html>
<body>
 
<canvas id="canvas" width="190" height="190"
style="background-color:var(--basalt-main-text-color)">
</canvas>
 
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var radius = canvas.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.90
setInterval(drawClock, 1000);
 
function drawClock() {
  drawFace(ctx, radius);
  drawNumbers(ctx, radius);
  drawTime(ctx, radius);
}
 
function drawFace(ctx, radius) {
  var grad;
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2*Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();
  grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
  grad.addColorStop(0, '#515154');
  grad.addColorStop(0.5, '#AD9EF9');
  grad.addColorStop(1, '#333');
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
  ctx.fillStyle = '#515154';
  ctx.fill();
}
 
function drawNumbers(ctx, radius) {
  var ang;
  var num;
  ctx.font = radius*0.15 + "px arial";
  ctx.textBaseline="middle";
  ctx.textAlign="center";
  for(num = 1; num < 13; num++){
    ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius*0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius*0.85);
    ctx.rotate(-ang);
  }
}
 
function drawTime(ctx, radius){
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    //hour
    hour=hour%12;
    hour=(hour*Math.PI/6)+
    (minute*Math.PI/(6*60))+
    (second*Math.PI/(360*60));
    drawHand(ctx, hour, radius*0.5, radius*0.07);
    //minute
    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
    drawHand(ctx, minute, radius*0.8, radius*0.07);
    // second
    second=(second*Math.PI/30);
    drawHand(ctx, second, radius*0.9, radius*0.02);
}
 
function drawHand(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.lineCap = "round";
    ctx.moveTo(0,0);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos);
}
</script>
 
</body>
</html>

倒计时器(自动更新)

<!DOCTYPE html>
<html>
<head>
  <title>天数倒计时</title>
  <style>
    #countdown {
      font-size: 24px;
      text-align: center;
    }
  </style>
</head>
<body>
  <h1>请在这里更改你的倒计时名称</h1>
  <div id="countdown"></div>
 
  <script>
    // 目标日期(以毫秒为单位)
    var targetDate = new Date('2023-10-1').getTime();
 
   //请在new Date('2023-10-1')更改倒计时最后一天
    function updateCountdown() {
      var now = new Date().getTime();
      var distance = targetDate - now;
 
      // 计算剩余天数
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
 
      document.getElementById('countdown').innerHTML = ' ' + days + ' 天';
 
      // 如果倒计时结束
      if (distance < 0) {
        clearInterval(interval);
        document.getElementById('countdown').innerHTML = '倒计时已结束';
      }
    }
 
    // 每秒更新一次倒计时
    var interval = setInterval(updateCountdown, 1000);
  </script>
</body>
</html>


🎄



🎄






🎊









🧧

🧧

🧧

🧧

🧨

🧨

🧨

🧨

📕

🏮

🏮

🏮

除非特别注明,本页内容采用以下授权方式: Creative Commons Attribution-ShareAlike 3.0 License