标签:style ar io color os sp on div cti
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js获取当前时间显示在页面上</title>
<script>
window.onload=function(){
//定时器每秒调用一次fnDate()
setInterval(function(){
fnDate();
},1000);
}
//js 获取当前时间
function fnDate(){
var oDiv=document.getElementById("div1");
var date=new Date();
var year=date.getFullYear();//当前年份
var month=date.getMonth();//当前月份
var data=date.getDate();//天
var hours=date.getHours();//小时
var minute=date.getMinutes();//分
var second=date.getSeconds();//秒
var time=year+"-"+fnW((month+1))+"-"+fnW(data)+" "+fnW(hours)+":"+fnW(minute)+":"+fnW(second);
oDiv.innerHTML=time;
}
//补位 当某个字段不是两位数时补0
function fnW(str){
var num;
str>10?num=str:num="0"+str;
return num;
}
</script>
</head>
<body>
<div id="div1"> </div>
</body>
</html>
标签:style ar io color os sp on div cti
原文地址:http://www.cnblogs.com/jalja/p/4171637.html