JavaScript Date对象

一、创建

1.1  new Date() 

var dt = new Date();
console.log(dt)
>>> Thu Aug 16 2018 21:15:29 GMT+0800 (中国标准时间)

1.2. new Date(milliseconds) :把毫秒数转换为Date对象

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

1、当前系统区域设置格式(toLocaleDateString和toLocaleTimeString)

     例子:(new Date()).toLocaleDateString() + " " + (new Date()).toLocaleTimeString()
     结果: 2008年1月29日 16:13:11

2.普通字符串(toDateString和toTimeString)

     例子: (new Date()).toDateString() + " " + (new Date()).toTimeString()
     结果:Tue Jan 29 2008 16:13:11 UTC+0800

3.格林威治标准时间(toGMTString)

     例子: (new Date()).toGMTString()
     结果:Tue, 29 Jan 2008 08:13:11 UTC

4.全球标准时间(toUTCString)

     例子: (new Date()).toUTCString()
     结果:Tue, 29 Jan 2008 08:13:11 UTC

5.Date对象字符串(toString)

     例子: (new Date()).toString()
     结果:Tue Jan 29 16:13:11 UTC+0800 2008

 1.3 new Date(dateStr) :把字符串转换为Date对象

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'); // 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

1.4 new Date(year, month, opt_day, opt_hours, opt_minutes, opt_seconds, opt_milliseconds) :把年月日、时分秒转换为Date对象

参数:

①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

2. 实例方法

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')
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')

3. 静态方法

1. Date.now()

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

参数:

返回值:

{int} :当前时间与起始时间之间的毫秒数。

console.log(Date.now()); // => 1419431519276

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(加上本地时区)。若不省略时间,此字符串在IE中返回NaN(非数字)!

返回值:

{int} 返回转换后的Date对象与起始时间之间的毫秒数。

console.log(Date.parse('2014/12/25 12:00:00')); // => 1419480000000
console.log(Date.parse('2014-12-25 12:00:00')); // => 1419480000000  (注意:此转换方式在IE中返回NaN!)
原文地址:https://www.cnblogs.com/geoffreyone/p/9899755.html