Javascript:从军事转换为标准时间

时间:2014-03-12 22:54:54

标签: javascript

我正在尝试将此军用时钟转换为标准时间。我是新的(只是学习)如何编码计算机,不知道该怎么做。网站,资源,想法将不胜感激。 谢谢!

   <?xml version="1.0" encoding="ISO-8859-1"?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">

   <head>
   <title>Digital Clock</title>
   <script type="text/javascript">
    <!--
    // this function actually updates the clock display. It will be called
    // every second provided by the setInterval() call in the document body
    function updateClock()
  {
  var time = new Date();              // create a new time stamp
  var hours = time.getHours();        // retrieve hours from the time stamp
  hours = format(hours);              // ... and format them

  var minutes = time.getMinutes();    // retrieve minutes from the time stamp
  minutes = format(minutes);          // ... and format them

  var seconds = time.getSeconds();    // retrieve seconds from the time stamp
  seconds = format(seconds);          // ... and format them

  // update the text field (the top clock)
  document.clock.display.value = hours + ":" + minutes + ":"
  + seconds;                  

  // update the button text (the bottom clock)
  document.clock.display2.value = hours + ":" + minutes; 
  }

 // this function adds a leading zero to the number if it is below 10
 function format(number)
 {
 if (number < 10)
 {
 number = "0" + number;
 }
  return number; 
  }
 //   -->
</script>
 </head>

 <body bgcolor="lightyellow">
 <h2>Digital Clock</h2>

 <form name="clock" action="#">
 <p><input type="text" size="8" name="display" value="12:00:00" /></p>
 <p><input type="button" name="display2" value="12:00:00" /></p>
  </form>

<script type="text/javascript">
setInterval("updateClock()", 1000);   // update the clocks every second
</script>

<p>More information on the digital clock design can be found 
<a href="time.html">here</a>.</p>

<p>You can examine the code of the project by clicking on <b>"View" -&gt;
"Source"</b> buttons in Internet Explorer.</p>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

行后:

  hours = format(hours);  

您应该可以使用以下内容:

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