1、Data对象
日期对象用于处理日期和时间
2、获取当日日期
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
var date=new Date();
document.write(date);
</script>
</body>
</html>
运行结果:
3、常用方法:
3.1 getFullYear(); 获取年份
代码如下:
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
var date=new Date();
document.write(date.getFullYear());
</script>
</body>
</html>
3.2 getTime(); 获取毫秒
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
var date=new Date();
document.write(date.getTime());
</script>
</body>
</html>
3.3 setFullYear(); 设置具体日期
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
var date=new Date();
document.write(date.setFullYear(2016,9,25));
document.write(date);
</script>
</body>
</html>
3.4 getDay(); 获取日期
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body>
<script>
var date=new Date();
document.write(date.getDay());
</script>
</body>
</html>
4、时钟小例子
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>对象</title>
</head>
<body onload="startTime()">
<script>
//小时钟
function startTime(){
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m=checkTime(m);
s=checkTime(s);
document.getElementById("timetxt").innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){
startTime();
},500);
}
function checkTime(i){
if(i<10){
i="0"+i;
}
return(i);
}
</script>
<div id="timetxt" ></div>
</body>
</html>
运行结果:数字在不断的变化,间隔为500ms