如何获得“0”分?在“1”;“9”;在一个钟里

How to get a "0" before "1" to "9" in a clock

本文关键字:一个 钟里 何获得      更新时间:2023-09-26

我的时钟脚本如下。由于某种原因,if(minutes<10){minutes="0"+minutes;}if(seconds<10){seconds="0"+seconds;}会在小于10的数字前添加0,但在数小时内不会这样做。有什么建议吗?

<script>
 function TimeUpdate() {
  var now = new Date(), hours = now.getHours(), minutes = now.getMinutes(), seconds = now.getSeconds();
  // The 1st "if" does not work.
  if (hours < 10) {hours = "0" + hours;}
  if (minutes < 10) {minutes = "0" + minutes;}
  if (seconds < 10) {seconds = "0" + seconds;}
  // (This works) AM or PM option
  if (hours >= 12 && hours < 24) {var TimeOfDay = "PM";}
  else {var TimeOfDay = "AM";}
  // (This works) Converts the hours from 24 to 12
  if (hours > 12) {hours = hours - 12;}
  // This sets the hours to a specific number.
  // This is used only for this demonstration.
  hours = 5;
  // (This works) Puts everything together
  var CurrentTime = hours + ':' + minutes + ':' + seconds + "&nbsp;" + TimeOfDay;
  // (This works) The clock <div>
  var MyClock = document.getElementById('clock');
  // (This works) Writes the "CurrentTime" to the clock's <div>
  MyClock.innerHTML = CurrentTime;
  var t = setInterval (function () {TimeUpdate ()}, 1000);
 }
 // (This works) This loads the clock onto the page.
 window.onload = TimeUpdate;
</script>
<p id="clock"></p>

你的支票写满了。把这段代码再往下移动:

function TimeUpdate() {
       var now = new Date(),
         hours = now.getHours(),
         minutes = now.getMinutes(),
         seconds = now.getSeconds();
       if (minutes < 10) {
         minutes = "0" + minutes;
       }
       if (seconds < 10) {
         seconds = "0" + seconds;
       }
       // (This works) AM or PM option
       if (hours >= 12 && hours < 24) {
         var TimeOfDay = "PM";
       } else {
         var TimeOfDay = "AM";
       }
       // (This works) Converts the hours from 24 to 12
       if (hours > 12) {
         hours = hours - 12;
       }
       // This sets the hours to a specific number.
       // This is used only for this demonstration.
       hours = 5;
       // The 1st "if" does not work.
       if (hours < 10) {
         hours = "0" + hours;
       }
       // (This works) Puts everything together
       var CurrentTime = hours + ':' + minutes + ':' + seconds + "&nbsp;" + TimeOfDay;
       // (This works) The clock <div>
       var MyClock = document.getElementById('clock');
       // (This works) Writes the "CurrentTime" to the clock's <div>
       MyClock.innerHTML = CurrentTime;
       var t = setInterval(function() {
         TimeUpdate()
       }, 1000);
     }
      // (This works) This loads the clock onto the page.
     window.onload = TimeUpdate;
<br>
<br>
<br>
<p id="clock"></p>

你需要放置

if (hours < 10) {
         hours = "0" + hours;
       }

if (hours > 12) {
         hours = hours - 12;
       }

因为它必须在13-21小时(1-9 PM)也是正确的。

注意这段代码。这是个递归函数,你很快就会耗尽你的电脑内存。请将TimeUpdate()从自身内部移除

function TimeUpdate() {
  var now = new Date(),
    hours = now.getHours(),
    minutes = now.getMinutes(),
    seconds = now.getSeconds();
  if (minutes < 10) {
    minutes = "0" + minutes;
  }
  if (seconds < 10) {
    seconds = "0" + seconds;
  }
  // (This works) AM or PM option
  if (hours >= 12 && hours < 24) {
    var TimeOfDay = "PM";
  } else {
    var TimeOfDay = "AM";
  }
  // (This works) Converts the hours from 24 to 12
  if (hours > 12) {
    hours = hours - 12;
  }
  // This sets the hours to a specific number.
  // This is used only for this demonstration.
  hours = 5;
  // The 1st "if" does not work.
  if (hours < 10) {
    hours = "0" + hours;
  }
  // (This works) Puts everything together
  var CurrentTime = hours + ':' + minutes + ':' + seconds + "&nbsp;" + TimeOfDay;
  // (This works) The clock <div>
  var MyClock = document.getElementById('clock');
  // (This works) Writes the "CurrentTime" to the clock's <div>
  MyClock.innerHTML = CurrentTime;
}
// (This works) This loads the clock onto the page.
var t = setInterval(function() {
  TimeUpdate()
}, 1000);
<br>
<br>
<br>
<p id="clock"></p>