944,042 Members | Top Members by Rank

Ad:
0

javascript clock

by on Mar 26th, 2007
This script displays the current time in your browser. You can choose the 12-hours format or the 24-hours format.
JavaScript / DHTML / AJAX Code Snippet (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <title>Javascript clock</title>
  4. <head>
  5. <style type="text/css">
  6. form,label
  7. {
  8. font-family:arial, sans-serif, helvetica, "times new roman";
  9. font-size:10pt; /* increase or decrease the value to change the size of the font */
  10. }
  11. h1
  12. {
  13. font-family:arial, sans-serif, helvetica, "times new roman";
  14. }
  15. </style>
  16.  
  17. <script type="text/javascript">
  18. <!-- hide the code from old browsers that do not support javascript
  19.  
  20. /*
  21.   Original: daniusr
  22.  
  23.  
  24.   You are welcomed to modify this script to your needs.
  25.  
  26.   This script retrieves the time from the internal system clock,
  27.   so it is up to you to make sure the time is accurate or correct.
  28.   */
  29.  
  30.  
  31. function clock()
  32. {
  33. var today = new Date();
  34. var hours = today.getHours();
  35. var minutes = today.getMinutes();
  36. var seconds = today.getSeconds();
  37. var time_holder; // holds the time
  38.  
  39. //if the first radio button is checked display 12-hours format time
  40. if(timeForm.stime[0].checked)
  41. {
  42. // add "AM" or "PM" if the 12-hours format is chosen
  43. var ampm = ((hours >= 12) ? " PM" : " AM");
  44.  
  45. // convert the hour to 12-hours format
  46. // javascript returns midnight as 0, but since the time is in the 12-hours format
  47. // force javascript to return 12
  48. hours = ((hours == 0) ? "12" : (hours > 12) ? hours - 12 : hours);
  49.  
  50. // add a leading zero if less than 10
  51. minutes = ((minutes < 10) ? "0" + minutes : minutes);
  52. seconds = ((seconds < 10) ? "0" + seconds : seconds);
  53.  
  54. time_holder = hours + ":" + minutes + ":" + seconds + ampm;
  55.  
  56. document.getElementById('jsClock').innerHTML = time_holder;
  57. }
  58. if(timeForm.stime[1].checked)
  59. {
  60. // add a leading zero if less than 10
  61. hours = ((hours < 10) ? "0" + hours : hours);
  62. minutes = ((minutes < 10) ? "0" + minutes : minutes);
  63. seconds = ((seconds < 10) ? "0" + seconds : seconds);
  64.  
  65. time_holder = hours + ":" + minutes + ":" + seconds;
  66.  
  67. document.getElementById('jsClock').innerHTML = time_holder;
  68. }
  69.  
  70.  
  71. if((!timeForm.stime[0].checked) && (!timeForm.stime[1].checked))
  72. {
  73.  
  74. document.getElementById('jsClock').innerHTML = "Please select a time format.";
  75. }
  76.  
  77.  
  78. // keep the clock ticking
  79. setTimeout("clock()", 1000);
  80. }
  81. // end hiding -->
  82. </script>
  83. </head>
  84.  
  85. <body onload="javascript:clock()">
  86.  
  87.  
  88. <h1 id="jsClock" style="margin:0" align="center"></h1>
  89.  
  90. <form name="timeForm" style="margin:0">
  91. time format:
  92. <label for="ci"><input type="radio" name="stime" id="ci">12-hours format</label>
  93. <label for="mi"><input type="radio" name="stime" id="mi">24-hours format</label>
  94. </form>
  95.  
  96.  
  97. </body>
  98. </html>
Comments on this Code Snippet
May 9th, 2008
0

Re: javascript clock

error is occured in line 40 char 7 timeForm.stime=0

repair it
Newbie Poster
fclose_fp is offline Offline
1 posts
since May 2008
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Javascript moving stuff
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Dalying Javascript code execution order





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC