AMONRA75

JS - LIVECLOCK

May 30th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.     $(document).ready(function(){
  3.    setInterval('updateClock()', 1000);
  4. });
  5.  
  6. function updateClock (){
  7.     var currentTime = new Date ( );
  8.     var currentHours = currentTime.getHours ( );
  9.     var currentMinutes = currentTime.getMinutes ( );
  10.     var currentSeconds = currentTime.getSeconds ( );
  11.  
  12.     // Pad the minutes and seconds with leading zeros, if required
  13.     currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  14.     currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  15.  
  16.     // Choose either "AM" or "PM" as appropriate
  17.     var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
  18.  
  19.     // Convert the hours component to 12-hour format if needed
  20.     currentHours = ( currentHours > 24 ) ? currentHours - 24 : currentHours;
  21.  
  22.     // Convert an hours component of "0" to "12"
  23.     currentHours = ( currentHours == 0 ) ? 24 : currentHours;
  24.  
  25.     // Compose the string for display
  26.     var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
  27.    
  28.    
  29.     $("#clock").html(currentTimeString);       
  30.  }
  31. </script>
Add Comment
Please, Sign In to add comment