国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript類型系統――日期Date對象全面了解

2019-11-20 09:30:09
字體:
來源:轉載
供稿:網友

前面的話

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對象的標準的日期時間字符串格式的字符串

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

以上這篇javascript類型系統――日期Date對象全面了解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇巴县| 黄梅县| 兴和县| 深泽县| 清新县| 扎兰屯市| 铜山县| 施秉县| 道真| 澳门| 万州区| 剑阁县| 民乐县| 寿宁县| 永德县| 濮阳市| 桂东县| 抚松县| 应用必备| 郁南县| 定州市| 孟村| 河间市| 镇原县| 巴林右旗| 洛宁县| 奎屯市| 景宁| 衡水市| 四子王旗| 织金县| 宁波市| 左贡县| 宁明县| 酒泉市| 徐水县| 澎湖县| 崇信县| 满城县| 靖西县| 泸西县|