本文實例講述了javascript獲取系統當前時間的代碼。分享給大家供大家參考。具體如下:
運行效果截圖如下:
具體代碼如下:
<!DOCTYPE html> <html> <head> <title>獲取時間</title> <script type="text/javascript"> window.onload = function(){ showTime(); } function showTime(){ var myDate = new Date(); var year = myDate.getFullYear(); var month = myDate.getMonth() + 1; var date = myDate.getDate(); var dateArr = ["日","一",'二','三','四','五','六']; var day = myDate.getDay(); var hours = myDate.getHours(); var minutes = formatTime(myDate.getMinutes()); var seconds = formatTime(myDate.getSeconds()); var systemTime = document.getElementById("time"); systemTime.innerHTML = " " + year + "年" + month +"月" + date + "日" + " 星期" + dateArr[day] + " " + hours + ":" + minutes + ":" + seconds; setTimeout("showTime()",500); } //格式化時間:分秒。 function formatTime (i){ if(i < 10){ i = "0" + i; } return i; } </script> </head> <body> <div id ='time'></div> </body> </html>
以上就是為大家分享的javascript獲得系統當前時間的方法,幫助大家更好的學習javascript的Date對象,希望大家喜歡。