HTML+JS实时显示时间
div
<
div
id=
"div_timer"
></
div
>
js
<
script
type=
"text/javascript"
>
//获取系统时间
function
showTime() {
nowtime =
new
Date();
year =
nowtime.
getFullYear();
//年
month =
nowtime.
getMonth() +
1;
//月
day =
nowtime.
getDate();
//日
hour =
nowtime.
getHours();
//时
minutes =
nowtime.
getMinutes();
//分
seconds =
nowtime.
getSeconds();
//秒
//文字增加空格
document.
getElementById(
"div_timer").
style =
"white-space:pre;";
//显示时间
document.
getElementById(
"div_timer").
innerText =
year +
"." +
p(
month) +
"." +
p(
day) +
" " +
p(
hour) +
":" +
p(
minutes) +
":" +
p(
seconds);
}
setInterval(
"showTime()",
1000);
//月日时分秒小于10补0
function
p(
s) {
return
s <
10 ?
'0' +
s :
s;
}
<
/
script
>