2023-09-02T11:15:20.png

<!DOCTYPE html>
<html>
  <head>
    <title>实时时间</title>
 
  </head>
  <body>
    <h1 id="datetime"></h1>
       <script>
      function updateDateTime() {
        var dateTimeElement = document.getElementById('datetime');
        var date = new Date();
        var year = date.getFullYear();
        var month = ("0" + (date.getMonth() + 1)).slice(-2);
        var day = ("0" + date.getDate()).slice(-2);
        var hours = ("0" + date.getHours()).slice(-2);
        var minutes = ("0" + date.getMinutes()).slice(-2);
        var seconds = ("0" + date.getSeconds()).slice(-2);
        var datetime = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        dateTimeElement.textContent = datetime;
      }

      // 更新时间显示
      updateDateTime();
      // 每秒更新一次时间显示
      setInterval(updateDateTime, 1000);
    </script>
  </body>
</html>