JavaScript Date对象

一、Date对象

       用法:Date对象用于处理日期和时间

    语法:var myDate = new Date()

    注:Date()会自动把当前日期和时间作为初始值

二、实例:用Date对象动态显示当前日期及时间

<html>
  <head>
    <script type="text/javascript">
       function startTime(){
           var today=new Date()             //返回当日的日期和时间。
           var y=today.getFullYear()        //从 Date 对象以四位数字返回年份。
           var mon=today.getMonth()+1       //从 Date 对象返回月份 (0 - 11)。
           var d=today.getDate()            //从 Date 对象返回一个月中的某一天 (1 - 31)。
           var h=today.getHours()           //返回 Date 对象的小时 (0 - 23)。
           var m=today.getMinutes()         //返回 Date 对象的分钟 (0 - 59)。
           var s=today.getSeconds()         //返回 Date 对象的秒数 (0 - 59)。
           // add a zero in front of numbers<10
           m=checkTime(m)
           s=checkTime(s)
           document.getElementById('txt').innerHTML=y+"年"+mon+"月"+d+"日"+h+":"+m+":"+s
           setTimeout('startTime()',500)
        }

        function checkTime(i){
           if (i<10){
              i="0" + i
           }
        return i
        }
    </script>
  </head>
  <body οnlοad="startTime();">
     <p id="txt"></p>
  </body>
</html>
三、其他常用Date对象方法

       1、getDay()
      用法:getDay()方法可返回表示星期的某一天的数字。返回dateObject所指的星期中的某一天,使用本地时间。返回值是0(周日)到6(周六)之间的一个整数。
      语法:DateObject.getDay()
    2、getUTCDate()
               用法: getUTCDate() 方法可根据世界时返回一个月 (UTC) 中的某一天。dateObject用世界时表示时,返回该月中的某一天(是1-31中的一个值)。
       语法:DateObject.getUTCDate()
    3、setDate()
               用法: setDate() 方法用于设置一个月的某一天。返回调整过的日期的毫秒表示。
       语法:DateObject.setDate(day)
    4、setFullYear()
               用法: setFullYear() 方法用于设置年份。返回调整过的日期的毫秒表示。
       语法:DateObject.setFullYear(year,month,day)
              参数:month和day为可选
    5、setHours()
               用法: setHours() 方法用于设置指定的时间的小时字段。返回调整过的日期的毫秒表示。
       语法:DateObject.setHours(hour,min,sec,millisec)
              参数:min、sec和millisec为可选
    6、toDateString()
      用法:toDateString() 方法可把 Date 对象的日期部分转换为字符串。返回dateObject 的日期部分的字符串表示,由实现决定,使用本地时间表示。
      语法:DateObject.toDateString()
      用法: toLocaleDateString() 方法可根据本地时间把 Date 对象的日期部分转换为字符串。返回dateObject 的日期部分的字符串表示,以本地时间区表示,并根据本地规则格式化。
      语法:DateObject.toLocaleDateString()

四、其他常用Date对象方法实例

<html>
  <head>
    
  </head>
  <body>
     <script type="text/javascript">
           var today=new Date()             
           document.write(today+"<br/>")                 //返回值:Thu May 08 2014 21:52:34 GMT+0800 (中国标准时间)
           document.write(today.getDay()+"<br/>")        //返回值:4,表示今天是周四(Thu)
           document.write(today.getUTCDate()+"<br/>")    //返回值:8
           
           today.setDate(15)
           document.write(today+"<br/>")           //返回值Thu May 15 2014 21:52:34 GMT+0800 (中国标准时间)

           today.setDate(32)
           document.write(today+"<br/>")           //返回值Sun Jun 01 2014 21:52:34 GMT+0800 (中国标准时间)

           today.setFullYear(2000,1,1)
           document.write(today+"<br/>")          //返回值Tue Feb 01 2000 21:52:34 GMT+0800 (中国标准时间)

           today.setFullYear(2000,13,1)
           document.write(today+"<br/>")         //返回值Thu Feb 01 2001 21:52:34 GMT+0800 (中国标准时间)

           today.setHours(23)
           document.write(today+"<br/>")         //返回值Thu Feb 01 2001 23:52:34 GMT+0800 (中国标准时间)

           today.setHours(23,23,61,888)
           document.write(today+"<br/>")         //返回值Thu Feb 01 2001 23:24:01 GMT+0800 (中国标准时间)
      
           document.write(today.toDateString()+"<br/>")        //返回值Thu Feb 01 2001
           document.write(today.toLocaleDateString()+"<br/>")  //返回值2001年2月1日
    </script>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值