javascript显示当前时间

时间:2011-11-22 20:22:27

标签: javascript

我有这个javascript代码来显示当前日期和时间:

<script type="text/javascript">
tday  =new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
tmonth=new Array("January","February","March","April","May","June","July","August","September","October","November","December");

function GetClock(){
d = new Date();
nday   = d.getDay();
nmonth = d.getMonth();
ndate  = d.getDate();
nyear = d.getYear();
nhour  = d.getHours();
nmin   = d.getMinutes();
nsec   = d.getSeconds();

if(nyear<1000) nyear=nyear+1900;
if(nhour ==  0) {ap = " AM";nhour = 12;} 
else if(nhour <= 11) {ap = " AM";} 
else if(nhour == 12) {ap = " PM";} 
else if(nhour >= 13) {ap = " PM";nhour -= 12;}

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+ap+"";
setTimeout("GetClock()", 1000);
}
window.onload=GetClock;
</script>
<div id="clockbox"></div>  

所以我想用一些图像替换“PM”和“AM”。有人可以帮我解决这个问题吗? 提前致谢!

1 个答案:

答案 0 :(得分:2)

if (ap == ' AM') {
    ap = " <img src='/path/to/am.png'>";
} else {
    ap = " <img src='/path/to/pm.png'>";
}

在自定义两个图像标记的src后,将其粘贴在document.getElementById行之前。