javascript implements the method of analog clock

  • 2020-06-12 08:25:24
  • OfStack

An example of javascript is presented in this paper. Share to everybody for everybody reference. Specific implementation methods are as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>javascript Analog clock </title>
 <!--Javascript Sample code explanation: 
  This example is used Javascript Built-in objects Date the getFullYear,
 getMonth and getDate Methods. The first statement 1 A variable d . var d = new Date() . 
  Assigns the date value of the day to a variable d . Then use the getFullYear I get the year, 
  with getMonth Get the monthly value ( Note: getMonth The return value range is 0 to 11 . 
  So to get the actual months, we have to add 1) . 
  with getDate Gets the date value of the month in which the date of the day is located. 
  This example is also used "test? statements 1: statements 2" . 
  That means if it fits test Condition, then execute the statement 1 . 
  Otherwise use a statement 2 . This syntax is used to calculate both the month and the day, 
  If the month and the day are less than 10 , should be added before the value of the month and day 0 . =-->
 <script type="text/javascript">
  function Format2Len(i) {
//   if (i < 10) {
//    return "0" + i;
//   }
//   else {
//    return i;
   //   }
   return i < 10 ? "0" + i : i;
  }
  function RefreshClock() {
   var CurrentTime = new Date();
   var timeStr = CurrentTime.getFullYear() + "-" +
     Format2Len(CurrentTime.getMonth()+1) + "-" +
     Format2Len(CurrentTime.getDate()) + " " +
     Format2Len(CurrentTime.getHours()) + ":" +
     Format2Len(CurrentTime.getMinutes()) + ":" +
     Format2Len(CurrentTime.getSeconds());
   txtClock.value = timeStr;
  }
  setInterval("RefreshClock()", 1000);
 </script>
</head>
<body onload="RefreshClock()">
<!-- Execute when the page loads 1 time -->
  The current time :<input type="text" id="txtClock" />
</body>
</html>

Hopefully, this article has been helpful in your javascript programming.


Related articles: