码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript中的Date

时间:2016-08-30 22:59:50      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:javascript


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>

效果图

技术分享







Javascript中的Date

标签:javascript

原文地址:http://lsieun.blog.51cto.com/9210464/1844449

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!