www.webdeveloper.com
Results 1 to 5 of 5

Thread: 12 hr and 24 hr clock need help asap

  1. #1
    Join Date
    Mar 2011
    Posts
    12

    12 hr and 24 hr clock need help asap

    Ok, I want to be able to display the a 12hr clock, or 24 hr clock depending on which radio button is selected, but I cannot get the code to work. Please help. My code:
    Code:
    <html>
    <head>
    <title>Assignment 9c Clock
    </title>
    </head>
    <body>
    <script type="text/javascript">
    var disp_field = document.getElementById("display");
    var radios = document.getElementsByName("format");
    var ampm;
    var format;
    var time;
    function run_clock(){
     date_inst = new Date();
     var hours = date_inst.getHours();
     var mins = date_inst.getMinutes();
     var seconds = date_inst.getSeconds();
     if (radios[0].checked){
      format = "12hour";
      if (hours > 12){
       hours -= 12;
       ampm = "P.M";
      }
      else{
       ampm = "A.M";
      }
     }
     else{
      format = "24hour";
     }
     if (seconds < 10){
      seconds = "0" + seconds;
     }
     if (format == "12hour"){
      time = hours+":"+mins+":"+seconds+" "+ampm;
     }
     else{
      time = hours+":"+mins+":"+seconds;
     }
     disp_field.value = time;
    }
    var repeat = window.setInterval("run_clock()",1000);
    </script>
    <div id="display"></div>
    <form>
    <input type="radio" id="format" name="format">format</input>
    <input type="radio" id="format" name="format">format</input>
    </form>
    </body>
    </html>
    Last edited by Kor; 03-18-2011 at 07:54 AM. Reason: wrap the code [code][/code]

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,259

    Lightbulb Something to consider ...

    Note changes to your code for future reference:
    Code:
    <html>
    <head>
    <title>Assignment 9c Clock</title>
    <script type="text/javascript">
    var radios = document.getElementsByName("format");
    var ampm;
    var format;
    function run_clock(){
      date_inst = new Date();
      var hours = date_inst.getHours();
      var mins = date_inst.getMinutes();
      var seconds = date_inst.getSeconds();
      format = "12hour";
    
      if (radios[0].checked) {
        if (hours > 12) { hours -= 12; ampm = "P.M"; }
                   else { ampm = "A.M"; }
      } else { format = "24hour"; ampm = 'Hours'; }
    
      if (mins < 10) { mins = "0" + mins; }
      if (seconds < 10) { seconds = "0" + seconds; }
      if (format == "12hour") { format = hours+":"+mins+":"+seconds+" "+ampm; }
                         else { format = hours+":"+mins+":"+seconds+" "+ampm; }
      document.getElementById('display').innerHTML = format;
    }
    var repeat = window.setInterval("run_clock()",1000);
    </script>
    </head>
    <body>
    <div id="display">Time</div>
    <form>
    <label><input type="radio" name="format" id="Reg" checked>Regular</input></label>
    <label><input type="radio" name="format" id="Mil">Military</input></label>
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Dec 2010
    Posts
    207
    You won't help yourself by continually starting new threads on the same trivial subject.
    The answer is the same as before: Use the error console.


    Q: I found this code...
    A: Then find its author.

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Quote Originally Posted by clueful View Post
    You won't help yourself by continually starting new threads on the same trivial subject.
    The answer is the same as before: Use the error console.
    Three entries for the same problem:
    THIS one
    http://www.webdeveloper.com/forum/sh...d.php?t=243724
    http://www.webdeveloper.com/forum/sh...d.php?t=243727

    Patience, codybonvo. There is not a staff of developers on here 24/7. It's mostly other developers from around the world, and we all have jobs and various other responsibilities. Your "rush" is not our problem. We will help as best we can.. unless you become an annoyance or a troll, in which case you'll most likely be ignored.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  5. #5
    Join Date
    Mar 2011
    Posts
    12
    ok I'm set thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles