Sunday 24 August 2014

Digital Clock

This is a pretty cool way to display the time on your web page using jQuery to get the current time and then refresh the clock every 1 second.




function Clock ( )     {  

  var currentTime = new Date ( ); 
   var currentHours = currentTime.getHours ( ); 
   var currentMinutes = currentTime.getMinutes ();   


 var currentSeconds = currentTime.getSeconds ( );  

 currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;    

currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;     
  
   var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";   
   currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;     

  currentHours = ( currentHours == 0 ) ? 12 : currentHours;  
 
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;     
           $("#clock").html(currentTimeString);
           }


  $(document).ready(function() {
    setInterval('Clock()', 1000);
});