javascript類型系統——日期Date對象

前面的話

  Date對象是javascript語言中內置的數據類型,用於提供日期和時間的操做接口。Date對象是在早期java中的java.util.Date類基礎上建立的,爲此,Date類型使用自UTC1970年1月1日0點開始通過的毫秒數來保存日期,它能夠表示的時間範圍是1970年1月1日0點先後的各1億天。本文將詳細介紹Date對象的用法javascript

 

靜態方法

  在介紹Date對象的構造函數以前,先介紹靜態方法。由於,Date對象的靜態方法與其構造函數有着千絲萬縷的聯繫。使用構造函數建立Date對象的過程,相似於披着外套的靜態方法的使用過程html

  Date對象總共有三個靜態方法,分別是Date.now()、Date.parse()、Date.UTC()。這些方法經過Date()構造函數自己調用,而不是經過Date實例對象java

Date.now()瀏覽器

  ECMAScript5新增了now()方法,該方法返回當前時間距離1970年1月1日0點UTC的毫秒數。該方法不支持傳遞參數函數

  [注意]該方法返回的是Number數字類型ui

console.log(Date.now());//1468297046050
console.log(Date.now('2016,1,1'));//1468297046050
console.log(typeof Date.now());//'number'

  在不支持Date.now()方法的瀏覽器中,能夠用+操做符把Date對象轉換成數字,也能夠實現相似效果spa

console.log(new Date());//Tue Jul 12 2016 12:21:33 GMT+0800 (中國標準時間)
console.log(+new Date());//1468297293433
console.log(+new Date(2000,1,1));//949334400000

  該方法經常使用於分析代碼的工做設計

var start = Date.now();
doSomething();
var stop = Date.now();
result = stop - start;

Date.parse()code

  該方法用於解析一個日期字符串,參數是一個包含待解析的日期和時間的字符串,返回從1970年1月1日0點到給定日期的毫秒數htm

  該方法會根據日期時間字符串格式規則來解析字符串的格式,除了標準格式外,如下格式也支持。若是字符串沒法識別,將返回NaN

  一、'月/日/年' 如6/13/2004

  二、'月 日,年' 如January 12,2004或Jan 12,2004

  三、'星期 月 日 年 時:分:秒 時區' Tue May 25 2004 00:00:00 GMT-0700

  [注意]瀏覽器不支持不表示日期只表示時間的字符串格式

console.log(Date.parse('6/13/2004'));//1087056000000
console.log(Date.parse('January 12,2004'));//1073836800000
console.log(Date.parse('Tue May 25 2004 00:00:00 GMT-0700'));//1085468400000
console.log(Date.parse('2004-05-25T00:00:00'));//1085443200000
console.log(Date.parse('2016'));//1451606400000
console.log(Date.parse('T00:00:00'));//NaN
console.log(Date.parse());//NaN

  [注意]在ECMAScript5中,若是使用標準的日期時間字符串格式規則的字符串中,數學前有前置0,則會解析爲UTC時間,時間沒有前置0,則會解析爲本地時間。其餘狀況通常都會解析爲本地時間

console.log(Date.parse('7/12/2016'));//1468252800000
console.log(Date.parse('2016-7-12'));//1468252800000
console.log(Date.parse('2016-07-12'));//1468281600000

Date.UTC()

  Date.UTC()一樣返回給定日期的毫秒數,但其參數並非一個字符串,而是分別表明年、月、日、時、分、秒、毫秒的數字參數

  Date.UTC(year,month,day,hours,minutes,seconds,ms),year和month參數是固定的,其他參數可選,日期時間格式規則詳見此

  由於該函數有7個形參,因此其length值爲7

console.log(Date.UTC.length);//7

  [注意]該方法使用的是UTC時間,而不是本地時間

console.log(Date.UTC(1970));//NaN
console.log(Date.UTC(1970,0));//0
console.log(Date.UTC(1970,0,2));//86400000
console.log(Date.UTC(1970,0,1,1));//3600000
console.log(Date.UTC(1970,0,1,1,59));//714000
console.log(Date.UTC(1970,0,1,1,59,30));//717000

 

構造函數

  Date()構造函數有多達5種的使用方法

  【0】Date()函數能夠不帶new操做符,像一個函數同樣調用。它將忽略全部傳入的參數,並返回當前日期和時間的一個字符串表示

    Date();

  [注意]因爲Date()函數沒有使用操做符,實際上它不能被稱爲構造函數

console.log(Date());//"Tue Jul 12 2016 13:38:41 GMT+0800 (中國標準時間)"
console.log(Date('2016/1/1'));//"Tue Jul 12 2016 13:38:41 GMT+0800 (中國標準時間)"
console.log(typeof Date());//'string'

  【1】Date()函數使用new操做符,且不帶參數時,將根據當前時間和日期建立一個Date對象

    new Date();
console.log(new Date());//Tue Jul 12 2016 13:41:45 GMT+0800 (中國標準時間)
console.log(new Date);//Tue Jul 12 2016 13:41:45 GMT+0800 (中國標準時間)
console.log(typeof new Date());//'object'

  【2】Date()函數可接受一個數字參數,該參數表示設定時間與1970年1月1日0點之間的毫秒數

new Date(milliseconds);
console.log(new Date(0));//Thu Jan 01 1970 08:00:00 GMT+0800 (中國標準時間)
console.log(new Date(86400000));//Fri Jan 02 1970 08:00:00 GMT+0800 (中國標準時間)
console.log(typeof new Date(0));//object

  【3】Date()函數可接受一個字符串參數,參數形式相似於Date.parse()方法。但parse()方法返回的是一個數字,而Date()函數返回的是一個對象 

new Date(datestring);
console.log(new Date('6/13/2004'));//Sun Jun 13 2004 00:00:00 GMT+0800 (中國標準時間)
console.log(Date.parse('6/13/2004'));//1087056000000
console.log(typeof new Date(6/13/2004));//object
console.log(typeof Date.parse(6/13/2004));//number

  關於標準的日期時間字符串中前置0的處理,也相似於Date.parse()方法,如有前置0,則至關於UTC時間,若沒有,則至關於本地時間。其他狀況通常都爲本地時間

console.log(new Date('7/12/2016'));//Tue Jul 12 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-7-12'));//Tue Jul 12 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-07-12'));//Tue Jul 12 2016 08:00:00 GMT+0800 (中國標準時間)

  【4】Date()函數可接受參數形式相似於Date.UTC()方法的參數,但Date.UTC()方法返回是一個毫秒數,且是UTC時間,而Date()函數返回是一個對象,且是本地時間

console.log(new Date(2016,7,12));//Fri Aug 12 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(+new Date(2016,7,12));//1470931200000
console.log(typeof new Date(2016,7,12));//'object'
console.log(Date.UTC(2016,7,12));//1470960000000
console.log(typeof Date.UTC(2016,7,12));//'number'

  [注意]使用參數相似於Date.parse()函數的方法時,若是日期對象超出範圍,瀏覽器會自動將日期計算成範圍內的值;使用參數相似於Date.UTC()函數的方法時,若是日期對象超出範圍,瀏覽器會提示Invalid Date

console.log(new Date(2016,7,32));//Thu Sep 01 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(new Date(2016,8,1));//Thu Sep 01 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-8-32'));//Invalid Date
console.log(new Date('2016-9-1'));//Thu Sep 01 2016 00:00:00 GMT+0800 (中國標準時間)

 

實例方法

  Date對象沒有能夠直接讀寫的屬性,全部對日期和時間的訪問都須要經過方法。Date對象的大多數方法分爲兩種形式:一種是使用本地時間,一種是使用UTC時間,這些方法在下面一塊兒列出。例如,get[UTC]Day()同時表明getDay()和getUTCDay()

  Date對象一共有46個實例方法,能夠分爲如下3類:to類、get類、set類

【to類】

  to類方法從Date對象返回一個字符串,表示指定的時間

toString()

  返回本地時區的日期字符串

toUTCString()

  返回UTC時間的日期字符串

toISOString()

  返回Date對象的標準的日期時間字符串格式的字符串

toDateString()

  返回Date對象的日期部分的字符串

toTimeString()

  返回Date對象的時間部分的字符串

toJSON()

  返回一個符合JSON格式的日期字符串,與toISOString方法的返回結果徹底相同

console.log(new Date('2016-7-12').toString());//Tue Jul 12 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-7-12').toUTCString());//Mon, 11 Jul 2016 16:00:00 GMT
console.log(new Date('2016-7-12').toISOString());//2016-07-11T16:00:00.000Z
console.log(new Date('2016-7-12').toDateString());//Tue Jul 12 2016
console.log(new Date('2016-7-12').toTimeString());//00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-7-12').toJSON());//2016-07-11T16:00:00.000Z

toLocaleString()

  toString()方法的本地化轉換

toLocaleTimeString()

  toTimeString()方法的本地化轉換

toLocaleDateString()

  toDateString()方法的本地化轉換

console.log(new Date('2016-7-12').toString());//Tue Jul 12 2016 00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-7-12').toLocaleString());//2016/7/12 上午12:00:00
console.log(new Date('2016-7-12').toDateString());//Tue Jul 12 2016
console.log(new Date('2016-7-12').toLocaleDateString());//2016/7/12
console.log(new Date('2016-7-12').toTimeString());//00:00:00 GMT+0800 (中國標準時間)
console.log(new Date('2016-7-12').toLocaleTimeString());//上午12:00:00

【get類】

  Date對象提供了一系列get類方法,用來獲取實例對象某個方面的值

  在介紹get類方法以前,首先要介紹valueOf()方法

valueOf()

  返回距離1970年1月1日0點的毫秒數

  所以,能夠方便地使用比較運算符來比較日期值

var date1 = new Date(2007,0,1);
var date2 = new Date(2007,1,1);
console.log(date1 > date2);//false
console.log(date1 < date2);//true

getTime()

  返回距離1970年1月1日0點的毫秒數,同valueOf()

  在ECMAScript5以前,可使用getTime()方法實現Date.now()

    Date.now = function(){
        return (new Date()).getTime()
    }

getTimezoneOffset()

  返回當前時間與UTC的時區差別,以分鐘表示(8*60=480分鐘),返回結果考慮到了夏令時因素

console.log(new Date('2016-7-12').valueOf());//1468252800000
console.log(new Date('2016-7-12').getTime());//1468252800000
console.log(new Date('2016-7-12').getTimezoneOffset());//-480

getYear()

  返回距離1900年的年數(已過期)

get[UTC]FullYear()

  返回年份(4位數)

get[UTC]Month()

  返回月份(0-11)

get[UTC]Date()

  返回第幾天(1-31)

get[UTC]Day()

  返回星期幾(0-6)

get[UTC]Hours()

  返回小時值(0-23)

get[UTC]Minutes()

  返回分鐘值(0-59)

get[UTC]Seconds()

  返回秒值(0-59)

get[UTC]Milliseconds()

  返回毫秒值(0-999)

  [注意]經過標準日期時間格式字符串,且有前置0的形式的參數設置,設置的是UTC時間

console.log(new Date('2016-07-12T10:00').getYear());//116
console.log(new Date('2016-07-12T10:00').getFullYear());//2016
console.log(new Date('2016-07-12T10:00').getUTCFullYear());//2016
console.log(new Date('2016-07-12T10:00').getMonth());//6
console.log(new Date('2016-07-12T10:00').getUTCMonth());//6
console.log(new Date('2016-07-12T10:00').getDate());//12
console.log(new Date('2016-07-12T10:00').getUTCDate());//12
console.log(new Date('2016-07-12T10:00').getDay());//2
console.log(new Date('2016-07-12T10:00').getUTCDay());//2
console.log(new Date('2016-07-12T10:00').getHours());//18
console.log(new Date('2016-07-12T10:00').getUTCHours());//10
console.log(new Date('2016-07-12T10:00').getMinutes());//0
console.log(new Date('2016-07-12T10:00').getUTCMinutes());//0
console.log(new Date('2016-07-12T10:00').getSeconds());//0
console.log(new Date('2016-07-12T10:00').getUTCSeconds());//0
console.log(new Date('2016-07-12T10:00').getMilliseconds());//0
console.log(new Date('2016-07-12T10:00').getUTCMilliseconds());//0
//當前時間爲16:35
console.log(new Date().getHours());//16
console.log(new Date().getUTCHours());//8

【set類】

  Date對象提供了一系列set類方法,用來設置實例對象的各個方面

  set方法基本與get方法相對應,set方法傳入相似於Date.UTC()的參數,返回調整後的日期的內部毫秒數

  [注意]星期只能獲取,不能設置

setTime()

  使用毫秒的格式,設置一個Date對象的值

var d = new Date('2016-07-12T10:00');
console.log(d.setTime(86400000),d);//86400000 Fri Jan 02 1970 08:00:00 GMT+0800 (中國標準時間)

setYear()

  設置年份(已過期)

var d = new Date('2016-07-12T10:00');
console.log(d.setYear(2000),d,d.getYear());//963396000000 Wed Jul 12 2000 18:00:00 GMT+0800 (中國標準時間) 100

set[UTC]FullYear()

  設置年份(4位數),以及可選的月份值和日期值

set[UTC]Month()

  設置月份(0-11),以及可選的日期值

set[UTC]Date()

  設置第幾天(1-31)

var d = new Date('2016-07-12T10:00');
console.log(d.setFullYear(2015,1,1),d.getFullYear());//1422784800000 2015
console.log(d.setMonth(2),d.getMonth());//1425204000000 2
console.log(d.setDate(20),d.getDate());//1426845600000 20
console.log(d.toLocaleString());//2015/3/20 下午6:00:00

set[UTC]Hours()

  設置小時值(0-23),以及可選的分鐘值、秒值及毫秒值

set[UTC]Minutes()

  設置分鐘值(0-59),以及可選的秒值及毫秒值

set[UTC]Seconds()

  設置秒值(0-59),以及可選的毫秒值

set[UTC]Milliseconds()

  設置毫秒值(0-999)

var d = new Date('2016-07-12T10:20:30');
console.log(d.setHours(1,2,3),d.getHours());//1468256523000 1
console.log(d.setMinutes(2,3),d.getMinutes());//1468256523000 2
console.log(d.setSeconds(3),d.getSeconds());//1468256523000 3
console.log(d.toLocaleTimeString())//上午1:02:03
var d = new Date('2016-07-12T10:20:30');
console.log(d.setUTCHours(1,2,3),d.getHours());//1468285323000  9
console.log(d.setUTCMinutes(2,3),d.getMinutes());//1468285323000  2
console.log(d.setUTCSeconds(3),d.getSeconds());//1468285323000  3
console.log(d.toLocaleTimeString())//上午9:02:03

 

參考資料

【1】 ES5/Date對象 https://www.w3.org/html/ig/zh/wiki/ES5/builtins#Date_.E5.AF.B9.E8.B1.A1
【2】 阮一峯Javascript標準參考教程——標準庫——Date對象 http://javascript.ruanyifeng.com/stdlib/date.html
【3】 W3School-Javascript高級教程——Date對象 http://www.w3school.com.cn/jsref/jsref_obj_date.asp【4】《javascript權威指南(第6版)》第三部分 javascript核心參考【5】《javascript高級程序設計(第3版)》第5章 引用類型

相關文章
相關標籤/搜索