JS中使用Date物件實時顯示系統時間小示例
阿新 • • 發佈:2019-02-05
JS中包含Date物件,其提供了一些方法獲取系統日期,直接上程式碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>獲取系統時間</title> <script language="JavaScript"> function realSysTime(clock) { var now = new Date(); var year = now.getFullYear(); //獲取年份 var month = now.getMonth(); //獲取月份 var date = now.getDate(); //獲取日期 var day = now.getDay(); //獲取星期 var hour = now.getHours(); //獲取小時 var minute = now.getMinutes(); //獲取分鐘 var seconds = now.getSeconds(); //獲取秒 month = month + 1; var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"); var week = arr_week[day]; var time = year + "年" + month + "月" + date + "日 " + week + " " + hour + ":" + minute + ":" + seconds; clock.innerHTML = "當前時間:" + time; } function show() { window.setInterval("realSysTime(clock)", 1000); } </script> </head> <body onload="show()" wind> <div id='clock'></div> </body> </html>