首页> 基础笔记 >JS/JQ基础学习 >对象 对象

JavaScript内置对象-日期对象定义及对象方法

作者:小萝卜 2023-03-21 浏览 215

简介日期对象可以使用系统内置的构造函数来创建日期对象:new Date()和new Date(str) //str表达日期的字符串,常用格式为“月 日,年 小时:分钟:秒”

日期对象可以使用系统内置的构造函数来创建日期对象:

new Date()

new Date(str) //str表达日期的字符串,常用格式为“月 日,年 小时:分钟:秒”


日期对象同样有constructor属性

日期对象方法:

getYear() //返回年份,不建议使用

getFullYear() //返回年份,4位数,建议使用

getMonth() //返回月份,其值范围为0~11

getDate() //返回日期对象中的一个月中的第几天

getDay() //返回星期中的某一天,0~6

getHours() //返回日期对象中的小时部分

getMinutes() //返回日期对象中的分钟部分

getSeconds() //返回日期对象中的秒钟部分

getMilliseconds() //返回日期对象中的毫秒部分

getTime() //返回日期对象中的时间戳的毫秒数

getTimezoneOffset() //返回日期对象中的时区的时差数,单位是秒

 

实例:
 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>JavaScript实例</title>
        <script type="text/javascript">
           
        </script>
    </head>
    <body>
        <h2>JavaScript实例:JS的日期对象 Date</h2>
        <div id="did" style="font-size:30px;"></div>
        <script type="text/javascript">
            function showdate(){
                  //获取当前日期对象
                  var date = new Date();
                  
                  //获取具体年月日时分秒
                  //date.setFullYear(2014);
                  var yy =date.getFullYear();
                  var mm =date.getMonth()+1;
                  var dd =date.getDate();
                  var hh =date.getHours();
                  var ii =date.getMinutes();
                  var ss =date.getSeconds();
                  
                  var str = yy+"-"+mm+"-"+dd+" "+hh+":"+ii+":"+ss;
                  
                  //document.write(str);
                  document.getElementById("did").innerHTML=str;
             }
             
             //定时每隔1秒调用一次
             setInterval(showdate,1000);
        </script>
    </body>
</html>

很赞哦! (0)

文章评论

    高端网站建设