基本概念
Date对象:javascript的Date对象用来处理日期和时间。
协调世界时(Universal Time Coordinated):简称UTC,由世界时同原子时协调而成的时间计算系统。它是当今世界无线电通讯中使用的标准时间。
格林威治标准时间(Greenwich Mean Time):简称GMT, 现在计算机和一些电子设备时间的计算和显示是以距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000)的偏移量为标准的。
- Date对象是基于1970年1月1日00:00:00时世界协调时(UTC)起的毫秒数,正负的范围为基准时间前后各1亿天
 
- 人们通常所说的中国标准时间,就是协调世界时UTC
 
- Date对象会自动把当前日期和时间保存为其初始值。
 
构造函数
1、 new Date();
1 2
   | new Date()              //如:Mon Jan 04 2016 13:47:57 GMT+0800 (中国标准时间) var today = new Date();
   | 
 
2、new Date(value)
value:代表自世界协调时1970年1月1日00:00:00 经过的毫秒数。
1 2
   | new Date(1451886597656); // Mon Jan 04 2016 13:49:57 GMT+0800 (中国标准时间) var Jan_04 = new Date(1451886597656);
   | 
 
3、new Date(dateString)
dateString:表示日期的字符串值。该字符串应该能被 Date.parse() 方法识别
推荐字符串格式:
1 2 3 4 5 6 7 8
   | new Date('2009/07/12');         // Sun Jul 12 2009 00:00:00 GMT+0800 (中国标准时间) new Date('2009/7/12');          // Sun Jul 12 2009 00:00:00 GMT+0800 (中国标准时间) new Date('2009/07/12 12:34');   // Sun Jul 12 2009 12:34:00 GMT+0800 (中国标准时间) new Date('2009/07/12 12:34:56');// Sun Jul 12 2009 12:34:56 GMT+0800 (中国标准时间)
  new Date('07/02/2012');         // Mon Jul 02 2012 00:00:00 GMT+0800 (中国标准时间) new Date('7/2/2012');           // Mon Jul 02 2012 00:00:00 GMT+0800 (中国标准时间) new Date('7/2/2012 12:34');     // Mon Jul 02 2012 12:34:00 GMT+0800 (中国标准时间)
  | 
 
4、new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]])
| 参数 | 
描述 | 
| year | 
代表年份的整数值。为了避免2000年问题最好指定4位数的年份; 使用 1998, 而不要用 98 | 
| month | 
月份,用从 0 到 11 的整数表示(1 月至 12 月) | 
| day | 
日期,用从 1 到 31 的整数表示 | 
| hour | 
小时,从 0 到 23 的整数(午夜到 11pm) | 
| minute | 
分钟,从 0 到 59 的整数 | 
| second | 
秒数,从 0 到 59 的整数 | 
| millisecond | 
毫秒, 从 0 到 999 的整数 | 
参数依次表示年、月、日、小时、分钟、秒和毫秒,这种格式至少需要提供 年月 两个参数,其他参数可选,默认为0。
1 2 3 4 5 6 7
   | new Date(2015, 0);                  // Thu Jan 01 2015 00:00:00 GMT+0800 (中国标准时间) new Date(2015, 0, 1, 0, 0, 0, 0);   // Thu Jan 01 2015 00:00:00 GMT+0800 (中国标准时间)
  //超出了正常范围 new Date(2014, 16)                  // Fri May 01 2015 00:00:00 GMT+0800 (中国标准时间) new Date(2014, -2)                  // Fri Nov 01 2013 00:00:00 GMT+0800 (中国标准时间) new Date(2014, 0, -1)               // Mon Dec 30 2013 00:00:00 GMT+0800 (中国标准时间)
   | 
 
Date 对象属性
Date.prototype:允许为 Date 实例对象添加属性。
Date.length:Date.length 的值是 7。这是该构造函数可接受的参数个数。
Date对象的静态方法
Date.now()
Date.now方法返回当前距离1970年1月1日 00:00:00 UTC的毫秒数(Unix时间戳乘以1000)
1 2 3 4
   | Date.now()                  // 如: 1451892741668
  毫秒更精确的时间,可精确到千分之一毫秒 window.performance.now()    // 如: 4634702.617000002
   | 
 
Date.parse()
Date.parse方法用来解析日期字符串,返回距离1970年1月1日 00:00:00的毫秒数。
1
   | Date.parse('Mon, 25 Dec 1995 13:30:00 +0430')   // 819882000000
  | 
 
Date.UTC()
默认情况下,Date对象返回的都是当前时区的时间,返回当前距离1970年1月1日 00:00:00 UTC的毫秒数。
1
   | Date.UTC(2011, 0, 1, 2, 3, 4, 567)
   | 
 
to类方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | //初始化日期 var d = new Date(2013, 0, 1);
  d.toString();               // Tue Jan 01 2013 00:00:00 GMT+0800 (中国标准时间) d.toLocaleString();         // 2013/1/1 上午12:00:00 d.toLocaleDateString();     // 2013/1/1 d.toLocaleTimeString();     // 上午12:00:00
  d.toDateString();           // Tue Jan 01 2013 d.toTimeString();           // 00:00:00 GMT+0800 (中国标准时间) d.toGMTString();            // Mon, 31 Dec 2012 16:00:00 GMT d.toUTCString();            // Mon, 31 Dec 2012 16:00:00 GMT          
  把一个日期转换为符合 ISO 8601 扩展格式的字符串 d.toISOString();            // 2012-12-31T16:00:00.000Z
   | 
 
get类方法
根据本地时
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | var d = new Date(2013, 0, 1, 1, 45, 35,400);
  d.getTime();                // 毫秒:356969600000 d.getFullYear();            // 年:2013 d.getMonth();               // 月: 0 d.getDate();                // 日:1 d.getHours();               // 时:1 d.getMinutes();             // 分:45 d.getSeconds();             // 秒: 35 getMilliseconds();          // 毫秒:400
  返回本地时间与格林威治标准时间 (GMT) 的分钟差 d.getTimezoneOffset()       // -480 d.getYear();                // 113
   | 
 
根据世界时
1 2 3 4 5 6 7 8 9
   | var d = new Date(2013, 0, 1, 1, 45, 35,400);    //2013/1/1 上午1:45:35
  d.getUTCFullYear();         // 年: 2012 d.getUTCMonth();            // 月: 11 d.getUTCDate();             // 日:31 d.getUTCHours();            // 时:17 d.getUTCMinutes();          // 分:45 d.getUTCSeconds();          // 秒:35 d.getUTCMilliseconds();     // 毫秒:400
   | 
 
set类方法
根据本地时
1 2 3 4 5 6 7 8 9 10
   | var d = new Date(2013, 0, 1, 1, 45, 35,400);
  d.setTime(1451896870207);       // Mon Jan 04 2016 16:41:10 GMT+0800 (中国标准时间) d.setFullYear(2018);            // Mon Jan 01 2018 01:45:35 GMT+0800 (中国标准时间) d.setMonth(8);                  // Sun Sep 01 2013 01:45:35 GMT+0800 (中国标准时间) d.setDate(16)                   // Wed Jan 16 2013 01:45:35 GMT+0800 (中国标准时间) d.setHours(20);                 // Tue Jan 01 2013 20:45:35 GMT+0800 (中国标准时间) d.setMinutes(46)                // Tue Jan 01 2013 01:46:35 GMT+0800 (中国标准时间) d.setSeconds(36)                // Tue Jan 01 2013 01:45:36 GMT+0800 (中国标准时间) d.setMilliseconds(500);         // 500
   | 
 
根据世界时
1 2 3 4 5 6 7 8 9
   | var d = new Date(2013, 0, 1, 1, 45, 35,400);    
  d.setUTCFullYear(2015)          // Fri Jan 01 2016 01:45:35 GMT+0800 (中国标准时间) d.setUTCMonth(1);               // Sat Mar 03 2012 01:45:35 GMT+0800 (中国标准时间) d.setUTCDate(13);               // Fri Dec 14 2012 01:45:35 GMT+0800 (中国标准时间) d.setUTCHours(3);               // Mon Dec 31 2012 11:45:35 GMT+0800 (中国标准时间) d.setUTCMinutes(54);            // Tue Jan 01 2013 01:54:35 GMT+0800 (中国标准时间) d.setUTCSeconds(44);            // Tue Jan 01 2013 01:45:44 GMT+0800 (中国标准时间) d.setUTCMilliseconds(100)       // 100
   | 
 
日期库
1、Datejs
Datejs是一个开源的JavaScript库解析日期,格式和处理。
地址:https://github.com/datejs/Datejs
2、Moment.js
JavaScript 日期处理类库
中文地址:http://momentjs.cn/
英文地址:http://momentjs.com/
3、jquery-countdown.js
jQuery倒计时是一个倒计时库
地址:https://github.com/Reflejo/jquery-countdown
- xdate.js
 
一个现代JavaScript库
地址:https://github.com/arshaw/xdate
参考链接
- Date-MDN
 
- w3school-Date
 
- 微软-Date
 
- 日期字符串解析