I am having a little trouble with this digital clock. It runs, but not the way I want it to and I can't find the problem. The problem I am having is when it's 12:00 pm(I dunno about am) it shows the hour as 0. I've set a variable to turn that 0 into 12, but maybe it's wrong?
Here's the script.
<html>
<head>
<script language="javascript">
function runningtime()
{
var now=new Date()
var ampm=now.getHours()
if (ampm > 12)
{nampm = "pm"}
else
{nampm = "am"}
var hr=now.getHours()
if (hr >=12)
{nhr = hr - 12}
else
{nhr=hr}
if (hr == 0)
{nhr = "12"}
else
{nhr = nhr}
var min=now.getMinutes()
if (min < 10)
{nmin="0" +min}
else
{nmin=min}
var sec=now.getSeconds()
if (sec < 10)
{nsec="0" + sec}
else
{nsec=sec}
if (nsec >= 60)
{nnsec="00"}
else
{nnsec=nsec}
var printit="The time is: " +nhr+ ":" +nmin+ ":" +nnsec+ ":" +nampm
document.clockform.clocktext.value=printit
setTimeout("runningtime()","1000")
}
</script>
</head>
<body bgcolor="black" onLoad="runningtime()">
<center><form name="clockform">
<input type="text" name="clocktext">
</form>
</body>
</html>
Here's the script.
<html>
<head>
<script language="javascript">
function runningtime()
{
var now=new Date()
var ampm=now.getHours()
if (ampm > 12)
{nampm = "pm"}
else
{nampm = "am"}
var hr=now.getHours()
if (hr >=12)
{nhr = hr - 12}
else
{nhr=hr}
if (hr == 0)
{nhr = "12"}
else
{nhr = nhr}
var min=now.getMinutes()
if (min < 10)
{nmin="0" +min}
else
{nmin=min}
var sec=now.getSeconds()
if (sec < 10)
{nsec="0" + sec}
else
{nsec=sec}
if (nsec >= 60)
{nnsec="00"}
else
{nnsec=nsec}
var printit="The time is: " +nhr+ ":" +nmin+ ":" +nnsec+ ":" +nampm
document.clockform.clocktext.value=printit
setTimeout("runningtime()","1000")
}
</script>
</head>
<body bgcolor="black" onLoad="runningtime()">
<center><form name="clockform">
<input type="text" name="clocktext">
</form>
</body>
</html>