0

Heres the final result. I'm updating it so that in the future, questions like the one I originally posted (how to make a 12 hour clock) can be forwarded to this thread for reference. Thanks to MrChief for his help!

<html>
<head>
    <script type="text/javascript">
        String.prototype.lpad = function(padString, length) {
        var str = this;
        while (str.length < length) {
        str = padString + str;
        }
        return str;
        }
        function timeNow() {
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        var tt = (h >= 12) ? " pm" : " am";
        time = (h - 12).toString().lpad("0", 2) + ":" + m.toString().lpad("0", 2) + ":" + s.toString().lpad("0", 2) + tt;
        document.getElementById('txt').innerHTML=time;
        var timer = setTimeout(timeNow,500);
        }
        </script></head>    
        <body onload="timeNow()">
            <div id="txt"></div>
        </body></html>
Chris
  • 1,881
  • 3
  • 20
  • 27
  • 5
    Welcome to SO! "Isn't working" is never a good problem description. What doesn't work out the way you planned? Do you see any errors in the error console? With that information, it becomes a lot easier to help. – Pekka Aug 16 '11 at 17:47
  • First of all, you have a lot of syntax errors. Try fixing those first then edit your question with the problems you are a having. – koralarts Aug 16 '11 at 17:52

4 Answers4

4

Maybe you meant

i="0" + i + "am";
          ^

and your checkTime function is missing closing parens.

Update:

There are better ways to do padding. Here's a function that modifies the string's prototype which adds a left padding function to string objects.

//pad left
String.prototype.lpad = function(padString, length) {
    var str = this;
    while (str.length < length)
       str = padString + str;
    return str;
}

Using that, your function becomes much simpler:

function timeNow() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();

    var tt = (h >= 12) ? " pm" : " am";
    time = h.toString().lpad("0", 2) + ":" + m + ":" + s.toString().lpad("0", 2) + tt;

    alert(time);
}

Demo: http://jsfiddle.net/mrchief/kTQnM/7/

Here's full demo using your HTML: http://jsfiddle.net/mrchief/kTQnM/10/

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • @user895051: Check my updated fiddle. Hopefully you'll find it simple enough! Also, your HTML doesn't have `clock' element. Plus it has an extra "`" at the end. – Mrchief Aug 16 '11 at 18:55
  • Ah, perfect. You solved the puzzle. Im going to update this post with the final result, and maybe a few comments so if this question ever gets on SO again you can just refer to this thread. Thanks oo much for your help! – Chris Aug 16 '11 at 19:03
2
  • function checkTime is missing its closing parenthesis.
  • you're suffixing am and pm to both your minutes and seconds.

Try running your JavaScript in an environment like jsFiddle during development.

Here's your code: http://jsfiddle.net/kTQnM/2/

enter image description here

To get AM/PM working as you need, suggest using this method: Converting 24 hour time to 12 hour time w/ AM & PM using Javascript

Community
  • 1
  • 1
p.campbell
  • 98,673
  • 67
  • 256
  • 322
0

You are trying to set am/pm from minutes and seconds rather than from hours. It should be a separate function for example

var am_pm = (h < 12) ? 'am' : 'pm';
m = (m < 10) ? '0' + m : m;
s = (s < 10) ? '0' + s : s;

Also, several syntax errors e.g. else (10<i<12) should be else if (i > 10 && i < 12)

Mark
  • 65
  • 1
  • 5
0

Change

 else (10<i<12)

to else (10 < i && i<12)

(1010, this becomes (true)<12 which will evaluate to true.

Hyangelo
  • 4,784
  • 4
  • 26
  • 33