Date类用来处理日期和时间,基于1970年1月1日(世界标准时间)起的毫秒数(时间戳)
1、创建日期对象
var now = new Date();//当前时间 var date = new Date(1000);//1970年1月1日起过了秒钟的时间 var date = new Date(year,month,day,hour,minute,second,millisecond);//通过分别指定各个时间分量来创建日期对象
2、提供的方法
getTime()获取一个Date对象所基于的时间戳
此外还提供了一系列的getter/setter方法来操作各个时间分量,如getHours()
注意:除了getMonth()(获取月份)的返回值是从0开始,其余都是从1开始
示例
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Javascript测试</title>
<script type="text/javascript">
var now = new Date();
alert(now);
alert("时间戳: " + now.getTime());
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate();
alert("年月日:" + year + "-" + (month<10?"0"+month:month) + "-" + (day<10?"0"+day:day));
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
alert("时分秒:" + hour + ":" + (minute<10?"0"+minute:minute) + ":" + (second<10?"0"+second:second));
</script>
</head>
<body>
</body>
</html>效果图
原文:http://lsieun.blog.51cto.com/9210464/1844449