User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 363,782 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,515 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Mar 26th, 2007
Views: 10,600
This script displays the current time in your browser. You can choose the 12-hours format or the 24-hours format.
javascript Syntax
  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 (Newest First)
fclose_fp | Newbie Poster | May 9th, 2008
error is occured in line 40 char 7 timeForm.stime=0

repair it
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 10:42 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC