如何获得" 0"之前" 1"至" 9"在一个时钟

时间:2015-07-29 16:50:22

标签: javascript html function onload clock

我的时钟脚本如下。出于某种原因,if(minutes<10){minutes="0"+minutes;}if(seconds<10){seconds="0"+seconds;}会在小于0的数字之前添加10,但它不会在数小时内执行此操作。关于如何解决它的任何建议?

<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>

3 个答案:

答案 0 :(得分:4)

您正在覆盖支票。进一步向下移动该代码块:

&#13;
&#13;
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;
&#13;
<br>
<br>
<br>
<p id="clock"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

你需要放置

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

之后

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

因为它必须在13-21(1-9 PM)的小时内成为现实。

答案 2 :(得分:1)

注意该代码。这是一个递归函数,你可以立刻吹掉你的计算机内存。请从内部删除<div> <h2>My List {{num}}</h2> <ul> <li *ng-for="#item of mySvc.myList">Item: {{item}}</li> </ul> </div>

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);