1 var oBody = document.body; 2 3 setInterval( fnTime, 1000 ); 4 5 fnTime (); 6 7 function fnTime () { 8 9 var myTime = new Date(); //object 10 11 // number 12 var iYear = myTime.getFullYear(); 13 var iMonth = myTime.getMonth()+1; 14 var iDate = myTime.getDate(); 15 var iWeek = myTime.getDay(); 16 var iHours = myTime.getHours(); 17 var iMin = myTime.getMinutes(); 18 var iSec = myTime.getSeconds(); 19 var str = ''; 20 21 if( iWeek === 0 ) iWeek = '星期日'; 22 if( iWeek === 1 ) iWeek = '星期一'; 23 if( iWeek === 2 ) iWeek = '星期二'; 24 if( iWeek === 3 ) iWeek = '星期三'; 25 if( iWeek === 4 ) iWeek = '星期四'; 26 if( iWeek === 5 ) iWeek = '星期五'; 27 if( iWeek === 6 ) iWeek = '星期六'; 28 29 str = iYear+ '年' +iMonth+'月'+iDate+'日 '+iWeek+' '+ toTwo(iHours)+' : '+ toTwo(iMin)+' : '+ toTwo(iSec); 30 31 oBody.innerHTML = str; 32 33 } 34 }; 35 36 function toTwo ( n ) { 37 return n < 10 ? '0' + n : '' + n; 38 }