-2

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>
asked Aug 16, 2011 at 17:46
2
  • 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. Commented Aug 16, 2011 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. Commented Aug 16, 2011 at 17:52

4 Answers 4

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/

answered Aug 16, 2011 at 17:50
Sign up to request clarification or add additional context in comments.

2 Comments

@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.
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!
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

answered Aug 16, 2011 at 17:50

Comments

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)

answered Aug 16, 2011 at 18:09

Comments

0

Change

 else (10<i<12)

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

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

answered Aug 16, 2011 at 18:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.