<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>获取系统时间</title> </head> <body> <div id="div1"> </div> <script type="text/javascript"> function addZero(val){ return val<10?("0"+val):val; } // 个位数补零 function getTimer(){ var nowtime = new Date(); var year = nowtime.getFullYear(); var month = nowtime.getMonth(); var day = nowtime.getDate(); var hours = nowtime.getHours(); var minute = nowtime.getMinutes(); var secon = nowtime.getSeconds(); //var msSecon = nowtime.getMilliseconds(); var week = nowtime.getDay(); var weekstr = "日一二三四五六" //getDay() 数字转换为中文的一二三四五等 var str = year+"年"+addZero(month+1)+"月"+addZero(day)+"日 星期"+weekstr.charAt(week)+" "+addZero(hours)+" : "+addZero(minute)+" : "+addZero(secon); // 字符串拼接 return str; } var oDiv = document.getElementById("div1"); oDiv.innerHTML = getTimer(); var timer = setInterval(function(){ var oDiv = document.getElementById("div1"); oDiv.innerHTML = getTimer(); },1000) // 定时触发 </script> </body> </html>
原文:http://www.cnblogs.com/zjx2011/p/4439515.html