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

來源:互聯網
上載者:User

標籤:參考型別   ons   代碼   程式設計   儲存   val   支援   now()   mon   

前面的話

  Date對象是javascript語言中內建的資料類型,用於提供日期和時間的操作介面。Date對象是在早期java中的java.util.Date類基礎上建立的,為此,Date類型使用自UTC1970年1月1日0點開始經過的毫秒數來儲存日期,它可以表示的時間範圍是1970年1月1日0點前後的各1億天。本文將詳細介紹Date對象的用法

 

靜態方法

  在介紹Date對象的建構函式之前,先介紹靜態方法。因為,Date對象的靜態方法與其建構函式有著千絲萬縷的聯絡。使用建構函式建立Date對象的過程,類似於披著外套的靜態方法的使用過程

  Date對象總共有三個靜態方法,分別是Date.now()、Date.parse()、Date.UTC()。這些方法通過Date()建構函式本身調用,而不是通過Date執行個體對象

Date.now()

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

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

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

  在不支援Date.now()方法的瀏覽器中,可以用+操作符把Date對象轉換成數字,也可以實作類別似效果

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

  該方法常用於分析代碼的工作

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

Date.parse()

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

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

  1、‘月/日/年‘ 如6/13/2004

  2、‘月 日,年‘ 如January 12,2004或Jan 12,2004

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

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

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

  [注意]在ECMAScript5中,如果使用標準的日期時間字串格式規則的字串中,數學前有前置0,則會解析為UTC時間,時間沒有前置0,則會解析為本地時間。其他情況一般都會解析為本地時間

console.log(Date.parse(‘7/12/2016‘));//1468252800000console.log(Date.parse(‘2016-7-12‘));//1468252800000console.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));//NaNconsole.log(Date.UTC(1970,0));//0console.log(Date.UTC(1970,0,2));//86400000console.log(Date.UTC(1970,0,1,1));//3600000console.log(Date.UTC(1970,0,1,1,59));//714000console.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‘));//1087056000000console.log(typeof new Date(6/13/2004));//objectconsole.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));//1470931200000console.log(typeof new Date(2016,7,12));//‘object‘console.log(Date.UTC(2016,7,12));//1470960000000console.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 Dateconsole.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 GMTconsole.log(new Date(‘2016-7-12‘).toISOString());//2016-07-11T16:00:00.000Zconsole.log(new Date(‘2016-7-12‘).toDateString());//Tue Jul 12 2016console.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:00console.log(new Date(‘2016-7-12‘).toDateString());//Tue Jul 12 2016console.log(new Date(‘2016-7-12‘).toLocaleDateString());//2016/7/12console.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);//falseconsole.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());//1468252800000console.log(new Date(‘2016-7-12‘).getTime());//1468252800000console.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());//116console.log(new Date(‘2016-07-12T10:00‘).getFullYear());//2016console.log(new Date(‘2016-07-12T10:00‘).getUTCFullYear());//2016console.log(new Date(‘2016-07-12T10:00‘).getMonth());//6console.log(new Date(‘2016-07-12T10:00‘).getUTCMonth());//6console.log(new Date(‘2016-07-12T10:00‘).getDate());//12console.log(new Date(‘2016-07-12T10:00‘).getUTCDate());//12console.log(new Date(‘2016-07-12T10:00‘).getDay());//2console.log(new Date(‘2016-07-12T10:00‘).getUTCDay());//2console.log(new Date(‘2016-07-12T10:00‘).getHours());//18console.log(new Date(‘2016-07-12T10:00‘).getUTCHours());//10console.log(new Date(‘2016-07-12T10:00‘).getMinutes());//0console.log(new Date(‘2016-07-12T10:00‘).getUTCMinutes());//0console.log(new Date(‘2016-07-12T10:00‘).getSeconds());//0console.log(new Date(‘2016-07-12T10:00‘).getUTCSeconds());//0console.log(new Date(‘2016-07-12T10:00‘).getMilliseconds());//0console.log(new Date(‘2016-07-12T10:00‘).getUTCMilliseconds());//0
//目前時間為16:35console.log(new Date().getHours());//16console.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 2015console.log(d.setMonth(2),d.getMonth());//1425204000000 2console.log(d.setDate(20),d.getDate());//1426845600000 20console.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 1console.log(d.setMinutes(2,3),d.getMinutes());//1468256523000 2console.log(d.setSeconds(3),d.getSeconds());//1468256523000 3console.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  9console.log(d.setUTCMinutes(2,3),d.getMinutes());//1468285323000  2console.log(d.setUTCSeconds(3),d.getSeconds());//1468285323000  3console.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章 參考型別

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.