JS中的Date对象

1、构造函数

Date 对象可以通过构造函数来生成,Date 的构造函数可以放入四种不同的参数

1.1、new Date() ,返回此时的本地日期时间的date对象

let d = new Date();
console.log(d);    //输出  Wed Jul 24 2019 10:26:37 GMT+0800 (中国标准时间)

 

1.2、new Date(毫秒数) ,返回一个通过毫秒数转变的date对象

参数里面的毫秒数是整数,表示的是从 '1970/01/01 00:00:00'  到所返回的 date 对象两者之间相隔的毫秒数。

注意:起点的时分秒还要加上当前所在的时区,如北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00' ,即 Thu Jan 01 1970 08:00:00 GMT+0800 (中国标准时间)

var d1 = new Date(1000 * 60 * 1); // 返回相比标准时间点前进了1分钟的date对象
console.log(d1); //输出  Thu Jan 01 1970 08:01:00 GMT+0800 (中国标准时间)  因为是在中国,所以是以东八区作为标准时间点的
d1 = new Date(-1000 * 3600 * 24); // 返回相比标准时间点倒退了1天的date对象
console.log(d1); //输出  Wed Dec 31 1969 08:00:00 GMT+0800 (中国标准时间)

 

1.3、new Date(format字符串),返回一个通过字符串转变的date对象

format 字符串的格式主要有两种

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

  2.  "yyyy-MM-dd HH:mm:ss" :若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区)。若不省略时间,此字符串在IE中会报错

 

var dt = new Date('2014/12/25');
console.log(dt); // Thu Dec 25 2014 00:00:00 GMT+0800 (中国标准时间)
dt = new Date('2014/12/25 12:00:00'); 
console.log(dt); // Thu Dec 25 2014 12:00:00 GMT+0800 (中国标准时间)
 
dt = new Date('2014-12-25');
console.log(dt); // Thu Dec 25 2014 08:00:00 GMT+0800 (中国标准时间)
dt = new Date('2014-12-25 12:00:00'); //  用这种方式不省略时间时在IE中会报错!
console.log(dt); //  Thu Dec 25 2014 12:00:00 GMT+0800 (中国标准时间)

 

 

1.4、new Date(year, month, day, hours, minutes, seconds, milliseconds),把年月日、时分秒转变成date对象

参数:

1)year:整数,4位或者2位数字。如:四位1999表示1999年,两位97 表示1979年

2)month:整数,2位数字。从0开始计算,0表示1月份、11表示12月份。

3)opt_day:整数,可选,2位数字

4)opt_hours:整数,可选,2位数字,范围0~23。

5)opt_minutes:整数,可选,2位数字,范围0~59。

6)opt_seconds:整数,可选,2位数字,范围0~59。

7)opt_milliseconds:整数,可选,范围0~999。

var dt = new Date(2014, 11);    // 2014年12月(这里输入的月份数字为11)
console.log(dt); // Mon Dec 01 2014 00:00:00 GMT+0800 (中国标准时间)

dt = new Date(2014, 11, 25); // 2014年12月25日
console.log(dt); // Thu Dec 25 2014 00:00:00 GMT+0800 (中国标准时间)

dt = new Date(2014, 12, 25); // 2014年13月25日(这里输入的月份数字为12,表示第13个月,跳转到第二年的1月)
console.log(dt); // Sun Jan 25 2015 00:00:00 GMT+0800 (中国标准时间)

dt = new Date(2014, 11, 25, 15, 30, 40); // 2014年12月25日 15点30分40秒
console.log(dt); // Thu Dec 25 2014 15:30:40 GMT+0800 (中国标准时间)

 

2、实例方法

2.1、get 方法

  1. getFullYear() :返回Date对象的年份值;4位年份。

  2. getMonth() :返回Date对象的月份值。从0开始,所以真实月份=返回值+1 。

  3. getDate() :返回Date对象的月份中的日期值;值的范围1~31 。

  4.  getHours() :返回Date对象的小时值。

  5. getMinutes() :返回Date对象的分钟值。

  6. getSeconds() :返回Date对象的秒数值。

  7. getMilliseconds() :返回Date对象的毫秒值。

  8. getDay() :返回Date对象的一周中的星期值;0为星期天,1为星期一、2为星期二,依此类推

  9. getTime() :返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00') 。

2.2、set 方法

  1. setFullYear(year, opt_month, opt_date) :设置Date对象的年份值;4位年份。

  2. setMonth(month, opt_date) :设置Date对象的月份值。0表示1月,11表示12月。

  3. setDate(date) :设置Date对象的月份中的日期值;值的范围1~31 。

  4. setHours(hour, opt_min, opt_sec, opt_msec) :设置Date对象的小时值。

  5. setMinutes(min, opt_sec, opt_msec) :设置Date对象的分钟值。

  6. setSeconds(sec, opt_msec) :设置Date对象的秒数值。

  7. setMilliseconds(msec) :设置Date对象的毫秒值。

2.3、其它方法

  1. toString() :将Date转换为一个'年月日 时分秒'字符串

  2. toLocaleString() :将Date转换为一个'年月日 时分秒'的本地格式字符串

  3. toDateString() :将Date转换为一个'年月日'字符串

  4.  toLocaleDateString() :将Date转换为一个'年月日'的本地格式字符串

  5.  toTimeString() :将Date转换为一个'时分秒'字符串

  6.  toLocaleTimeString() :将Date转换为一个'时分秒'的本地格式字符串

  7. valueOf() :与getTime()一样, 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00') 

 

参考:https://www.cnblogs.com/polk6/p/4156595.html#Menu5-Eg

 

 

posted @ 2019-07-24 11:04  wenxuehai  阅读(1385)  评论(0编辑  收藏  举报
//右下角添加目录