当前位置:Web前端 > javascript> 正文

js Date对象

时间:2015-3-9类别:Web前端

js Date对象

js Date对象

一、Date对象的创建

 

1、var objDate=new Date();   //Date 对象自动使用当前的日期和时间作为其初始值。

2、var objDate=new Date(milliseconds);    //将给定的毫秒数转换为使用的时间

参数:

①milliseconds {int} :毫秒数;表示从'1970/01/01 00:00:00'为起点,开始叠加的毫秒数。

注意:起点的时分秒还要加上当前所在的时区,北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00'

  • 
    
    var dt = new Date(1000 * 60 * 1); // 前进1分钟的毫秒数
    
    console.log(dt); // => {Date}:1970/01/01 08:01:00
    
    dt = new Date(-1000 * 60 * 1); // 倒退1分钟的毫秒数
    
    console.log(dt); // => {Date}:1970/01/01 07:59:00
    
    		
  • 3、new Date(dateStr)   //把字符串转换为Date对象

    参数:

    ①dateStr {string} :可转换为Date对象的字符串(可省略时间);字符串的格式主要有两种:

    1) yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的Date对象的时间为 00:00:00。

    2) yyyy-MM-dd HH:mm:ss :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。

  • 
    
    var dt = new Date('2014/12/25'); // yyyy/MM/dd
    
    console.log(dt); // => {Date}:2014/12/25 00:00:00
    
    dt = new Date('2014/12/25 12:00:00'); // yyyy/MM/dd HH:mm:ss
    
    console.log(dt); // => {Date}:2014/12/25 12:00:00
    
     
    
    dt = new Date('2014-12-25'); // yyyy-MM-dd
    
    console.log(dt); // => {Date}:2014-12-25 08:00:00 (加上了东8区的时区)
    
    dt = new Date('2014-12-25 12:00:00'); // yyyy-MM-dd HH:mm:ss (注意:此转换方式在IE中会报错!)
    
    console.log(dt); // => {Date}:2014-12-25 12:00:00
    
    		
  • 4、var objDate=new Date((yearmonthdate[hours[minutes[seconds[,ms]]]]));   //指定具体的日期

    参数:

    ①year {int} :年份;4位数字。如:1999、2014

    ②month {int} :月份;2位数字。从0开始计算,0表示1月份、11表示12月份。

    ③opt_day {int} 可选:号; 2位数字;从1开始计算,1表示1号。

    ④opt_hours {int} 可选:时;2位数字;取值0~23。

    ⑤opt_minutes {int} 可选:分;2位数字;取值0~59。

    ⑥opt_seconds {int} 可选:秒;2未数字;取值0~59。

    ⑦opt_milliseconds {int} 可选:毫秒;取值0~999。

  • 
    
    var dt = new Date(2014, 11); // 2014年12月(这里输入的月份数字为11) 
    
    console.log(dt); // => {Date}:2014/12/01 00:00:00
    
    dt = new Date(2014, 11, 25); // 2014年12月25日
    
    console.log(dt); // => {Date}:2014/12/25 00:00:00
    
    dt = new Date(2014, 11, 25, 15, 30, 40); // 2014年12月25日 15点30分40秒
    
    console.log(dt); // => {Date}:2014/12/25 15:30:40
    
    dt = new Date(2014, 12, 25); // 2014年13月25日(这里输入的月份数字为12,表示第13个月,跳转到第二年的1月)
    
    console.log(dt); // => {Date}:2015/01/25
    
    		
  •  

    二、get方法

     

    方法 描述
    getFullYear() 返回四位数的年份(如:2013)
    getYear() 根据浏览器的不同返回两位或者四位数的年份
    getMonth() 返回用整数表示的月份,从0(1月)到11(12月)
    getDate() 返回日期,从1开始
    getDay() 返回星期几,从0(星期日)到6(星期六)
    getHours() 返回小时数,从0到23(24小时制)
    getMinutes() 返回分钟数,从0到59
    getSeconds() 返回秒数,从0到59
    getMilliSeconds() 返回毫秒数,从0到999
    getTime() 返回从GMT时间1970年1月1日0点0分0秒经过的毫秒数

    例如

  •  
  • JScript 代码   复制
  • 
    
    dt.getFullYear(); // => 2014:年
    
    dt.getMonth(); // => 11:月;实际为12月份(月份从0开始计算)
    
    dt.getDate(); // => 25:日
    
    dt.getHours(); // => 15:时
    
    dt.getMinutes(); // => 30:分
    
    dt.getSeconds(); // => 40:秒
    
    dt.getMilliseconds(); // => 333:毫秒
    
    dt.getDay(); // => 4:星期几的值
    
    dt.getTime(); // => 1419492640333 :返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
    
    		
  •  

    三、set方法

     

    方法 描述
    setFullYear(year, opt_month, opt_date) 设置Date对象的年份值;4位年份
    setMonth(month, opt_date) 设置Date对象的月份值。0表示1月,11表示12月
    setDate(date) 设置Date对象的月份中的日期值;值的范围1~31
    setHours(hour, opt_min, opt_sec, opt_msec) 设置Date对象的小时值
    setMinutes(min, opt_sec, opt_msec) 设置Date对象的分钟值
    setSeconds(sec, opt_msec) 设置Date对象的秒数值
    setMilliseconds(msec) 设置Date对象的毫秒值

    实例

  •  
  • JScript 代码   复制
  • 
    
    var dt = new Date();
    
    dt.setFullYear(2014); // => 2014:年
    
    dt.setMonth(11); // => 11:月;实际为12月份(月份从0开始计算)
    
    dt.setDate(25); // => 25:日
    
    dt.setHours(15); // => 15:时
    
    dt.setMinutes(30); // => 30:分
    
    dt.setSeconds(40); // => 40:秒
    
    dt.setMilliseconds(333); // => 333:毫秒
    
    console.log(dt); // =>  2014年12月25日 15点30分40秒 333毫秒
    
    		
  •  

    四、Date对象转换成字符串

     

    方法 描述
    toString() 将Date转换为一个'年月日 时分秒'字符串
    toLocaleString() 将Date转换为一个'年月日 时分秒'的本地格式字符串
    toDateString() 将Date转换为一个'年月日'字符串
    toLocaleDateString() 将Date转换为一个'年月日'的本地格式字符串
    toTimeString() 将Date转换为一个'时分秒'字符串
    toLocaleTimeString() 将Date转换为一个'时分秒'的本地格式字符串

    实例

  •  
  • JScript 代码   复制
  • 
    
    var dt = new Date();
    
    console.log(dt.toString()); // => Tue Dec 23 2014 22:56:11 GMT+0800 (中国标准时间) :将Date转换为一个'年月日 时分秒'字符串
    
    console.log(dt.toLocaleString()); // => 2014年12月23日 下午10:56:11  :将Date转换为一个'年月日 时分秒'的本地格式字符串
    
     
    
    console.log(dt.toDateString()); // => Tue Dec 23 2014 :将Date转换为一个'年月日'字符串
    
    console.log(dt.toLocaleDateString()); // => 2014年12月23日 :将Date转换为一个'年月日'的本地格式字符串
    
     
    
    console.log(dt.toTimeString()); // => 22:56:11 GMT+0800 (中国标准时间) :将Date转换为一个'时分秒'字符串
    
    console.log(dt.toLocaleTimeString()); // => 下午10:56:11 :将Date转换为一个'时分秒'的本地格式字符串
    
     
    
    console.log(dt.valueOf()); // => 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00') 
    
    		
  •  

    五、静态方法

     

    1、Date.now()

    返回当前日期和时间的Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00') 

     

    2、Date.parse(dateStr)

    把字符串转换为Date对象 ,然后返回此Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')

    参数:

    ①dateStr {string} :可转换为Date对象的字符串(可省略时间);字符串的格式主要有两种:

    1) yyyy/MM/dd HH:mm:ss (推荐):若省略时间,返回的Date对象的时间为 00:00:00。

    2) yyyy-MM-dd HH:mm:ss :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。

     

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐