Reputation: 3
Just need a quick tweak if thats ok. I got this code from the web and would just like to add an am/pm to be displayed at the end of the clock/time. Many thanks...
<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
<div id="txt"></div>
Upvotes: 0
Views: 1866
Reputation: 68
Or, if you also want it displayed in 12-hour format:
<script type="text/javascript">
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
var hd=h;
document.getElementById('txt').innerHTML=(hd=0?"12":hd>12?hd-12:hd)+":"+m+":"+s+" "+(h<12?"AM":"PM");
t=setTimeout(function(){startTime()},500);
}
function checkTime(i)
{
if (i<10)
{
i="0" + i;
}
return i;
}
</script>
Upvotes: 1
Reputation: 769
This should do the trick.
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
var suffix;
if (h >= 12 && h < 24){
suffix = "pm";
} else {
suffix = "am";
}
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s + " " + suffix;
t = setTimeout(function () {
startTime()
}, 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
Upvotes: 0