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>
-
5Welcome 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– Pekka2011年08月16日 17:47:53 +00:00Commented 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.koralarts– koralarts2011年08月16日 17:52:37 +00:00Commented Aug 16, 2011 at 17:52
4 Answers 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/
2 Comments
clock' element. Plus it has an extra "" at the end.function checkTimeis missing its closing parenthesis.- you're suffixing
amandpmto 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
Comments
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)
Comments
Change
else (10<i<12)
to else (10 < i && i<12)
(1010, this becomes (true)<12 which will evaluate to true.