date类
var date = new Date();//当前日期
var year = date.getFullYear();//年份
var month = date.getMonth()+1;//月份(从0开始的所以加1)
var day = date.getDate();//哪一天 (1-31)
var house = date.getHours();//时 24小时制
var min = date.getMinutes();//分
var sec = date.getSeconds();//秒
var d = year+"-"+month+"-"+day+" "+house+":"+min+":"+sec;//组合时间
console.log(d);
定时器
<html>
<head>
<script>
var time;
(function setSpan(){
console.log(222);
time = setInterval(getDate,1000);
})();
function getDate(){
var s = document.getElementById("s");
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var house = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
month = month<10?"0"+month:month;
day = day<10?"0"+day:day;
house = house<10?"0"+house:house;
min = min<10?"0"+min:min;
sec = sec<10?"0"+sec:sec;
var d = year+"-"+month+"-"+day+" "+house+":"+min+":"+sec;
s.innerHTML = d;
}
//清空定时器
function clears(){
clearTimeout(time);
}
</script>
</head>
<body>
<span id="s"></span>
<button onclick="clears()">清除定时器</button>
</body>
</html>
常用对象String
案例
Array对象
案例
Math对象
Number对象