最近有个网站页面上要输出当前时间,精确到时分秒,还要适时变化的。百度找了找,没找到合适的,于是自己写了个,收藏下,方便以后使用。
//js 适时输出当前的时间 function CurentTime(divID){ var curTime=new Array(); var now=new Date(); var week=['日','一','二','三','四','五','六']; var year=now.getFullYear(); //年 var month=now.getMonth()+1; //月 var day=now.getDate(); //日 var hh=now.getHours(); //时 var mm=now.getMinutes(); //分 var sc=now.getSeconds(); //秒 var wk=now.getDay(); //周 curTime['year']=year; curTime['month']=month<10?'0'+month:month; curTime['day']=day<10?'0'+day:day; curTime['hh']=hh<10?'0'+hh:hh; curTime['mm']=mm<10?'0'+mm:mm; curTime['sc']=sc<10?'0'+sc:sc; curTime['wk']='星期'+week[wk]; curTime=curTime['year']+'年'+curTime['month']+'月'+curTime['day']+'日'+' '+curTime['wk']+' '+curTime['hh']+':'+curTime['mm']+':'+curTime['sc']; document.getElementById(divID).innerHTML='今天是:'+curTime; setTimeout('CurentTime(\''+divID+'\')',1000); }
使用方法:
//假设页面上存在一个id命名为time的div,则: <script language="javascript">CurentTime('time');</script>