原生js写的一个当前年份日期星期和时间的显示
话不多说,所有代码如下:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>当前时间显示 </title> </head> <body onload="startTime()"> <div id="time"></div> </body> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() var myyear=today.getFullYear(); var mymonth=today.getMonth(); var monthArray=["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"] var mynowdate=today.getDate(); var myday=today.getDay(); var dayArray=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"] m=checkTime(m) s=checkTime(s) document.getElementById('time').innerHTML=myyear+"年"+monthArray[mymonth]+mynowdate+"日"+dayArray[myday]+h+":"+m+":"+s setTimeout('startTime()',1000) } function checkTime(i) { if (i<10) {i="0" + i} return i } </script> </html>
耐得住寂寞,守得住繁华