Javascript – How to show a dynamic clock on master page

asp.netcjavascript

I tried it and searched a while on Google but there are the examples which are very complicated for me. I want to show a clock on my master page which should be dynamically. I am not much familiar with Jquery and all the examples I got there are with Jquery. So is there a simple way for a beginner to use that digital clock on master page. I also want to show the blink of the colon per tick which will separate the time.

Best Answer

Making a simple clock with javascript is not that difficult.

Have a look here for an example: example

I changed the example in the link a little so it's easier to read and perhaps to understand:

  <input type="text" id="clock"/>
  <script type="text/javascript">
    setInterval("settime()", 1000);

    function settime() 
    {
      var dateTime = new Date();
      var hour = dateTime.getHours();
      var minute = dateTime.getMinutes();
      var second = dateTime.getSeconds();

      if (minute < 10)
        minute = "0" + minute;

      if (second < 10)
        second = "0" + second;

      var time = "" + hour + ":" + minute + ":" + second;

      document.getElementById("clock").value = time;
    }
  </script>