Date 物件可以取得系統時間,使用方法如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function time() {
// 建立 Array 物件
var weekday = new Array("星期日", "星期一", "星期二",
"星期三", "星期四", "星期五", "星期六");
// 建立 Date 物件
var today = new Date();
// 透過 getFullYear() 方法取得年份
var output = today.getFullYear() + "/";
// 透過 getMonth() 方法取得月份,0~11 分別代表一至十二月
output += (today.getMonth() + 1) + "/";
// 透過 getDate() 方法取得日期
output += today.getDate() + "<br>";
// 輸出系統日期
document.write("系統日期:" + output);
// 透過 getHours() 方法取得小時
output = today.getHours() + ":";
// 透過 getMinutes() 方法取得分鐘
output += today.getMinutes() + ":";
// 透過 getSeconds() 方法取得秒數
output += today.getSeconds() + "<br>";
// 輸出系統時間
document.write("系統時間:" + output);
// 透過 getDay() 方法取得星期值,0~6 分別代表星期日到六
output = today.getDay();
//輸出系統星期值
document.write(weekday[output]);
}
</script>
</head>
<body onload="time()">
</body>
</html>