javascript類型系統(tǒng)--日期Date對象全面了解
來源:易賢網(wǎng) 閱讀:668 次 日期:2016-07-28 14:27:10
溫馨提示:易賢網(wǎng)小編為您整理了“javascript類型系統(tǒng)--日期Date對象全面了解”,方便廣大網(wǎng)友查閱!

前面的話

Date對象是javascript語言中內(nèi)置的數(shù)據(jù)類型,用于提供日期和時間的操作接口。Date對象是在早期java中的java.util.Date類基礎(chǔ)上創(chuàng)建的,為此,Date類型使用自UTC1970年1月1日0點開始經(jīng)過的毫秒數(shù)來保存日期,它可以表示的時間范圍是1970年1月1日0點前后的各1億天。本文將詳細介紹Date對象的用法

靜態(tài)方法在介紹Date對象的構(gòu)造函數(shù)之前,先介紹靜態(tài)方法。因為,Date對象的靜態(tài)方法與其構(gòu)造函數(shù)有著千絲萬縷的聯(lián)系。使用構(gòu)造函數(shù)創(chuàng)建Date對象的過程,類似于披著外套的靜態(tài)方法的使用過程

Date對象總共有三個靜態(tài)方法,分別是Date.now()、Date.parse()、Date.UTC()。這些方法通過Date()構(gòu)造函數(shù)本身調(diào)用,而不是通過Date實例對象

Date.now()

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

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

console.log(Date.now());//1468297046050

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

console.log(typeof Date.now());//'number'

在不支持Date.now()方法的瀏覽器中,可以用+操作符把Date對象轉(zhuǎn)換成數(shù)字,也可以實現(xiàn)類似效果

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

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

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

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

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

3、'星期 月 日 年 時:分:秒 時區(qū)' 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中,如果使用標準的日期時間字符串格式規(guī)則的字符串中,數(shù)學(xué)前有前置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()同樣返回給定日期的毫秒數(shù),但其參數(shù)并不是一個字符串,而是分別代表年、月、日、時、分、秒、毫秒的數(shù)字參數(shù)

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

因為該函數(shù)有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

構(gòu)造函數(shù)Date()構(gòu)造函數(shù)有多達5種的使用方法

【0】Date()

數(shù)可以不帶new操作符,像一個函數(shù)一樣調(diào)用。它將忽略所有傳入的參數(shù),并返回當前日期和時間的一個字符串表示

Date();

[注意]由于Date()函數(shù)沒有使用操作符,實際上它不能被稱為構(gòu)造函數(shù)

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()函數(shù)使用new操作符,且不帶參數(shù)時,將根據(jù)當前時間和日期創(chuàng)建一個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()函數(shù)可接受一個數(shù)字參數(shù),該參數(shù)表示設(shè)定時間與1970年1月1日0點之間的毫秒數(shù)

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()函數(shù)可接受一個字符串參數(shù),參數(shù)形式類似于Date.parse()方法。但parse()方法返回的是一個數(shù)字,而Date()函數(shù)返回的是一個對象 

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

關(guān)于標準的日期時間字符串中前置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()函數(shù)可接受參數(shù)形式類似于Date.UTC()方法的參數(shù),但Date.UTC()方法返回是一個毫秒數(shù),且是UTC時間,而Date()函數(shù)返回是一個對象,且是本地時間

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'

[注意]使用參數(shù)類似于Date.parse()函數(shù)的方法時,如果日期對象超出范圍,瀏覽器會自動將日期計算成范圍內(nèi)的值;使用參數(shù)類似于Date.UTC()函數(shù)的方法時,如果日期對象超出范圍,瀏覽器會提示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對象的大多數(shù)方法分為兩種形式:一種是使用本地時間,一種是使用UTC時間,這些方法在下面一起列出。例如,get[UTC]Day()同時代表getDay()和getUTCDay()

Date對象一共有46個實例方法,可以分為以下3類:to類、get類、set類

【to類】

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

toString()

返回本地時區(qū)的日期字符串

toUTCString()

返回UTC時間的日期字符串

toISOString()

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

toTimeString()

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

toJSON()

返回一個符合JSON格式的日期字符串,與toISOString方法的返回結(jié)果完全相同

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()方法的本地化轉(zhuǎn)換

toLocaleTimeString()

toTimeString()方法的本地化轉(zhuǎn)換

toLocaleDateString()

toDateString()方法的本地化轉(zhuǎn)換

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對象提供了一系列g(shù)et類方法,用來獲取實例對象某個方面的值

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

valueOf()

返回距離1970年1月1日0點的毫秒數(shù)

因此,可以方便地使用比較運算符來比較日期值

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點的毫秒數(shù),同valueOf()

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

Date.now = function(){

    return (new Date()).getTime()

  }

getTimezoneOffset()

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

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年的年數(shù)(已過時)

get[UTC]FullYear()

返回年份(4位數(shù))

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的形式的參數(shù)設(shè)置,設(shè)置的是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類方法,用來設(shè)置實例對象的各個方面

set方法基本與get方法相對應(yīng),set方法傳入類似于Date.UTC()的參數(shù),返回調(diào)整后的日期的內(nèi)部毫秒數(shù)

[注意]星期只能獲取,不能設(shè)置

setTime()

使用毫秒的格式,設(shè)置一個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()

設(shè)置年份(已過時)

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

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

set[UTC]Month()

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

set[UTC]Date()

設(shè)置第幾天(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()

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

set[UTC]Minutes()

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

set[UTC]Seconds()

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

set[UTC]Milliseconds()

設(shè)置毫秒值(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

以上這篇javascript類型系統(tǒng)——日期Date對象全面了解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:javascript類型系統(tǒng)--日期Date對象全面了解
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)