js如何獲得系統時間年月日時分秒
阿新 • • 發佈:2018-02-05
targe onload 調用方法 space m3u baidu function www. pyw
javascript 自帶有個對象(構造函數),Date().下面是代碼:
回答一:
var
now =
new
Date();
var
nowTime = now.toLocaleString();
var
date = nowTime.substring(0,10);
//截取日期
var
time = nowTime.substring(10,20);
//截取時間
var
week = now.getDay();
//星期
var
hour = now.getHours();
//小時
回答二:
var
date =
new
Date();
var
year = date.getFullYear();
var
month = date.getMonth()+1;
var
day = date.getDate();
var
hour = date.getHours();
var
minute = date.getMinutes();
var
second = date.getSeconds();
alert(year+
‘年‘
+month+
‘月‘
+day+
‘日 ‘
+hour
‘:‘
+minute+
‘:‘
+second)
回答三:
function
showTime() {
var
now =
new
Date();
var
nowTime = now.toLocaleString();
var
date = nowTime.substring(0,10);
//截取日期
var
time = nowTime.substring(10,20);
//截取時間
var
week = now.getDay();
//星期
var
hour = now.getHours();
//小時
//判斷星期幾
var
weeks = [
"日"
,
"一"
,
"二"
,
"三"
,
"四"
,
"五"
,
"六"
];
var
getWeek =
"星期"
+ weeks[week];
var
sc;
//判斷是AM or PM
if
(hour >= 0 && hour < 5){
sc =
‘淩晨‘
;
}
else
if
(hour > 5 && hour <= 7){
sc =
‘早上‘
;
}
else
if
(hour > 7 && hour <= 11){
sc =
‘上午‘
;
}
else
if
(hour > 11 && hour <= 13){
sc =
‘中午‘
;
}
else
if
(hour> 13 && hour <= 18){
sc =
‘下午‘
;
}
else
if
(hour > 18 && hour <= 23){
sc =
‘晚上‘
;
}
document.getElementById(
‘time‘
).innerHTML =
"當前時間:"
+ date+
" "
+ getWeek +
" "
+
" "
+sc+
" "
+time;
setTimeout(
‘showTime()‘
,1000);
}
</script>
調用方法:
<body onload=
"showTime()"
>
<div id=
"time"
></div>
</body>
js如何獲得系統時間年月日時分秒