ElGringo
11-14-2003, 08:30 PM
I've had this javascript clock in my files for awhile now and recently added it to a website. The problem is, it only works in the PM, never the AM. I always get this message...
"ampm is undefined". I haven't used javascript in quite awhile,
any help is appreciated.
<script language="JavaScript">
function clock(){
var today;
today=new Date();
date=today.getDate();
hours=today.getHours();
minutes=today.getMinutes();
seconds=today.getSeconds();
if (hours>12)
(hours<12)? ampm="AM":ampm="PM";
(ampm=="PM")? hours=hours-12:hours=hours;
if (hours<10)
hours="0"+hours;
if (minutes<10)
minutes="0"+minutes;
if (seconds<10)
seconds="0"+seconds;
weekdayname=today.getDay();
switch (weekdayname){
case 0: name="domingo";
break;
case 1: name="lunes";
break;
case 2: name="martes";
break;
case 3: name="miércoles";
break;
case 4: name="jueves";
break;
case 5: name="viernes";
break;
case 6: name="sábado";
}
monthname=new Array("enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre");
document.form1.month.value=monthname[today.getMonth()];
datename=new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
document.form1.date.value=datename[today.getDate()];
document.form1.day.value=name;
document.form1.clock.value=hours + ":" + minutes + ":" + seconds + ampm;
setTimeout("clock()",1000);
}
clock();
</script>
"ampm is undefined". I haven't used javascript in quite awhile,
any help is appreciated.
<script language="JavaScript">
function clock(){
var today;
today=new Date();
date=today.getDate();
hours=today.getHours();
minutes=today.getMinutes();
seconds=today.getSeconds();
if (hours>12)
(hours<12)? ampm="AM":ampm="PM";
(ampm=="PM")? hours=hours-12:hours=hours;
if (hours<10)
hours="0"+hours;
if (minutes<10)
minutes="0"+minutes;
if (seconds<10)
seconds="0"+seconds;
weekdayname=today.getDay();
switch (weekdayname){
case 0: name="domingo";
break;
case 1: name="lunes";
break;
case 2: name="martes";
break;
case 3: name="miércoles";
break;
case 4: name="jueves";
break;
case 5: name="viernes";
break;
case 6: name="sábado";
}
monthname=new Array("enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre");
document.form1.month.value=monthname[today.getMonth()];
datename=new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30);
document.form1.date.value=datename[today.getDate()];
document.form1.day.value=name;
document.form1.clock.value=hours + ":" + minutes + ":" + seconds + ampm;
setTimeout("clock()",1000);
}
clock();
</script>