Originally, I was going to post a question about how to condense four repetitive functions into a single function, but I wound up figuring it out myself before I could even post it.
However, the original idea that I had was to break down some of the repetitive sections into their own function and use those new sub-functions as part of the main one. I'm still not sure how I would have been able to make that work if I hadn't figured out the bigger picture.
function GetClock(){
var d=new Date().addHours(1);
var nday=d.getDay(),nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getYear();
if(nyear<1000) nyear+=1900;
var nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;
if(nhour===0){ap=" ";nhour=12;}
else if(nhour<12){ap=" ";}
else if(nhour===12){ap=" •";}
else if(nhour>12){ap=" •";nhour-=12;}
if(nhour<=9) nhour="0"+nhour;
if(nmin<=9) nmin="0"+nmin;
if(nsec<=9) nsec="0"+nsec;
document.getElementById('clockbox').innerHTML=""+tday[nday]+", "+tmonth[nmonth]+" "+ndate+", "+nyear+" "+nhour+":"+nmin+":"+nsec+"<span class='ampm'>"+ap+"</span>";
}
This is one of the sections that I was originally thinking about splitting off into its own function:
if(nhour===0){ap=" ";nhour=12;}
else if(nhour<12){ap=" ";}
else if(nhour===12){ap=" •";}
else if(nhour>12){ap=" •";nhour-=12;}
These were my two feeble attempts to make it work:
function ampm() {
if(nhour===0){ap=" ";nhour=12;}
else if(nhour<12){ap=" ";}
else if(nhour===12){ap=" •";}
else if(nhour>12){ap=" •";nhour-=12;}
}
And then this:
function ampm(nhr,ampm) {
if(nhr===0){ampm=" ";nhr=12;}
else if(nhr<12){ampm=" ";}
else if(nhr===12){ampm=" •";}
else if(nhr>12){ampm=" •";nhr-=12;}
}
But they both failed due to (what I'm assuming are) issues with scope that I'm not sure how to solve.