Js中Date物件
Js中Date物件
JavaScript
的Date
物件是用於處理日期和時間的全域性物件,Date
物件基於Unix Time Stamp
,即自1970
年1
月1
日UTC
起經過的毫秒數。
描述
Date()
建構函式能夠接受四種形式的引數,分別為沒有引數、Unix
時間戳、時間戳字串、分別提供日期與時間的每一個成員。此外建立一個新Date
物件的唯一方法是通過new
操作符,若將它作為常規函式呼叫,即不加new
操作符,將返回一個字串,而非Date
物件。
- 沒有引數: 如果沒有提供引數,那麼新建立的Date物件表示例項化時刻的日期和時間。
Unix
時間戳: 一個Unix
時間戳Unix Time Stamp
,它是一個整數值,表示自1970
1
月1
日00:00:00 UTC-the Unix epoch
以來的毫秒數,忽略了閏秒,請注意大多數Unix
時間戳功能僅精確到最接近的秒。- 時間戳字串: 表示日期的字串值,該字串應該能被
Date.parse()
正確方法識別,即符合IETF-compliant RFC 2822 timestamps
或version of ISO8601
。 - 分別提供日期與時間的每一個成員: 當至少提供了年份與月份時,這一形式的
Date()
返回的Date
物件中的每一個成員都來自提供的引數,沒有提供的成員將使用最小可能值,對日期為1
,其他為0
。
new Date(); new Date(value); new Date(dateString); new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
console.log(new Date()); // Sun Oct 18 2020 10:46:54 GMT+0800 (中國標準時間) console.log(new Date(1602989155183)); // Sun Oct 18 2020 10:45:55 GMT+0800 (中國標準時間) console.log(new Date("2020-10-18 10:15:30")); // Sun Oct 18 2020 10:15:30 GMT+0800 (中國標準時間) console.log(new Date(2020, 9, 18, 10, 15, 30)); // Sun Oct 18 2020 10:15:30 GMT+0800 (中國標準時間) console.log(typeof(Date())); // string console.log(typeof(new Date())); // object
方法
Date.UTC()
Date.UTC(year,month[,date[,hrs[,min[,sec[,ms]]]]])
Date.UTC()
方法接受的引數同日期建構函式接受最多引數時一樣,返回從1970-1-1 00:00:00 UTC
到指定日期的的毫秒數。
year
:1900
年後的某一年份。month
:0
到11
之間的一個整數,表示月份。date
:1
到31
之間的一個整數,表示某月當中的第幾天。hrs
:0
到23
之間的一個整數,表示小時。min
:0
到59
之間的一個整數,表示分鐘。sec
:0
到59
之間的一個整數,表示秒。ms
:0
到999
之間的一個整數,表示毫秒。
console.log(Date.UTC(2020, 9, 18, 10, 15, 30)); // 1603016130000
Date.now()
Date.now()
Date.now()
方法返回自1970
年1
月1
日00:00:00 (UTC)
到當前時間的毫秒數。
console.log(Date.now()); // 1602991355257
Date.parse()
Date.parse(dateString)
Date.parse()
方法解析一個表示某個日期的字串,該字串dateString
需要符合RFC2822
或ISO 8601
日期格式的字串(其他格式也許也支援,但結果可能與預期不符),並返回從1970-1-1 00:00:00 UTC
到該日期物件即該日期物件的UTC
時間的毫秒數,如果該字串無法識別,或者一些情況下,包含了不合法的日期數值例如2015-02-31
,則返回值為NaN
。不推薦在ES5
之前使用Date.parse
方法,因為字串的解析完全取決於實現。直到至今,不同瀏覽器在如何解析日期字串上仍存在許多差異,因此最好還是手動解析日期字串,在需要適應不同格式時庫能起到很大幫助。
console.log(Date.parse("2020-10-18 10:15:30")); // 1602987330000
console.log(Date.parse("2020-10-18 02:15:30 GMT")); // 1602987330000
console.log(Date.parse("2020-10-18 02:15:30 UTC")); // 1602987330000
console.log(Date.parse("2020-10-18 10:15:30 GMT")); // 1603016130000
console.log(Date.parse("2020-10-18 10:15:30 UTC")); // 1603016130000
console.log((1602987330000 - 1603016130000) / 1000 / 3600); // -8
// 如果你在格林威治,你的起始時間是1970年01月01日00時00分00秒。
// 如果你在中國北京,你的起始時間是1970年01月01日08時00分00秒。
// 以相同日期時間來算,北京地區的時間戳在量上是少的。
// 在不同的時區同時執行Date.now(),返回的時間戳是相同的,時間戳是不帶有時區資訊的。
// 如果我在北京獲取到一個時間戳 t,在格林威治使用new Date(t)的話,他取得的時間就比我慢 8 小時。
// 換個角度,如果以相同的時間戳來計算時間的話,北京時間超出格林威治標準時間 8 小時。
Date.prototype.getDate()
dateObj.getDate()
根據本地時間,返回一個指定的日期物件為一個月中的哪一日,範圍為從1-31
。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getDate()); // 18
Date.prototype.getDay()
dateObj.getDay()
getDay()
方法根據本地時間,返回一個具體日期中一週的第幾天,0
表示星期天。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getDay()); // 0
Date.prototype.getFullYear()
dateObj.getFullYear()
getFullYear()
方法根據本地時間返回指定日期的年份。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getFullYear()); // 2020
Date.prototype.getHours()
dateObj.getHours()
getHours()
方法根據本地時間,返回一個指定的日期物件的小時。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getHours()); // 10
Date.prototype.getMilliseconds()
dateObj.getMilliseconds()
getMilliseconds()
方法,根據本地時間,返回一個指定的日期物件的毫秒數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getMilliseconds()); // 0
Date.prototype.getMinutes()
dateObj.getMinutes()
getMinutes()
方法根據本地時間,返回一個指定的日期物件的分鐘數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getMinutes()); // 15
Date.prototype.getMonth()
dateObj.getMonth()
根據本地時間,返回一個指定的日期物件的月份,為基於0
的值,0
表示一年中的第一月。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getMonth()); // 9
Date.prototype.getSeconds()
dateObj.getSeconds()
getSeconds()
方法根據本地時間,返回一個指定的日期物件的秒數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getSeconds()); // 30
Date.prototype.getTime()
dateObj.getTime()
getTime
方法的返回值一個數值,表示從1970
年1
月1
日0
時0
分0
秒,距離該日期物件所代表時間的毫秒數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getTime()); // 1602987330000
Date.prototype.getTimezoneOffset()
dateObj.getTimezoneOffset()
getTimezoneOffset()
方法返回協調世界時UTC
相對於當前時區的時間差值,單位為分鐘。
var date = new Date();
console.log(date.getTimezoneOffset() / 60); // -8
Date.prototype.getUTCDate()
dateObj.getUTCDate()
getUTCDate()
方法以世界時為標準,返回一個指定的日期物件為一個月中的第幾天。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCDate()); // 18
Date.prototype.getUTCDay()
dateObj.getUTCDay()
getUTCDay()
方法以世界時為標準,返回一個指定的日期物件為一星期中的第幾天,其中0
代表星期天。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCDay()); // 0
Date.prototype.getUTCFullYear()
dateObj.getUTCFullYear()
getUTCFullYear()
以世界時為標準,返回一個指定的日期物件的年份。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCFullYear()); // 2020
Date.prototype.getUTCHours()
dateObj.getUTCHours()
getUTCHours()
方法以世界時為標準,返回一個指定的日期物件的小時數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCHours()); // 2
Date.prototype.getUTCMilliseconds()
dateObj.getUTCMilliseconds()
getUTCMilliseconds()
方法以世界時為標準,返回一個指定的日期物件的毫秒數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCMilliseconds()); // 0
Date.prototype.getUTCMinutes()
dateObj.getUTCMinutes()
getUTCMinutes()
方法以世界時為標準,返回一個指定的日期物件的分鐘數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCMinutes()); // 15
Date.prototype.getUTCMonth()
dateObj.getUTCMonth()
getUTCMonth()
方法以世界時為標準,返回一個指定的日期物件的月份,它是從0
開始計數的,0
代表一年的第一個月。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCMonth()); // 9
Date.prototype.getUTCSeconds()
dateObj.getUTCSeconds()
getUTCSeconds()
方法以世界時為標準,返回一個指定的日期物件的秒數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.getUTCSeconds()); // 30
Date.prototype.setDate()
dateObj.setDate(dayValue)
setDate()
方法根據本地時間來指定一個日期物件的天數。
dayValue
: 表示一個整數,表示該月的第幾天。
var date = new Date("2020-10-18 10:15:30");
date.setDate(1);
console.log(date); // Thu Oct 01 2020 10:15:30 GMT+0800 (中國標準時間)
Date.prototype.setFullYear()
dateObj.setFullYear(yearValue[, monthValue[, dayValue]])
setFullYear()
方法根據本地時間為一個日期物件設定年份。
yearValue
: 指定年份的整數值,例如1995。monthValue
: 一個0
到11之間的整數值,表示從一月到十二月。dayValue
: 一個1
到31
之間的整數值,表示月份中的第幾天,如果指定了dayValue
引數,就必須同時指定monthValue
。
var date = new Date("2020-10-18 10:15:30");
date.setFullYear(2019, 0, 1);
console.log(date); // Tue Jan 01 2019 10:15:30 GMT+0800 (中國標準時間)
Date.prototype.setHours()
dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
setHours()
方法根據本地時間為一個日期物件設定小時數,返回從1970-01-01 00:00:00 UTC
到更新後的日期物件例項所表示時間的毫秒數,在JavaScript 1.3
版本之前只接受一個引數。
hoursValue
: 一個0
到23
的整數,表示小時。minutesValue
: 一個0
到59
的整數,表示分鐘。secondsValue
: 一個0
到59
的整數,表示秒數,如果指定了secondsValue
引數,則必須同時指定minutesValue
引數。msValue
: 一個0
到999
的數字,表示微秒數,如果指定了msValue
引數,則必須同時指定minutesValue
和secondsValue
引數。
var date = new Date("2020-10-18 10:15:30");
console.log(date.setHours(8)); // 1602980130000
console.log(date); // Sun Oct 18 2020 08:15:30 GMT+0800 (中國標準時間)
Date.prototype.setMilliseconds()
dateObj.setMilliseconds(millisecondsValue)
setMilliseconds()
方法會根據本地時間設定一個日期物件的毫秒數。
millisecondsValue
: 一個0
到999
的數字,表示毫秒數。
var date = new Date("2020-10-18 10:15:30");
date.setMilliseconds(1);
console.log(date.getMilliseconds()); // 1
Date.prototype.setMinutes()
dateObj.setMinutes(minutesValue[, secondsValue[, msValue]])
setMinutes()
方法根據本地時間為一個日期物件設定分鐘數,在JavaScript 1.3
版本之前只接受第一個引數。
minutesValue
: 一個0
到59
的整數,表示分鐘數。secondsValue
: 一個0
到59
的整數,表示秒數。如果指定了secondsValue
引數,則必須同時指定minutesValue
引數。msValue
: 一個0
到999
的數字,表示微秒數,如果指定了msValue
引數,則必須同時指定minutesValue
和secondsValue
引數。
var date = new Date("2020-10-18 10:15:30");
date.setMinutes(1);
console.log(date); // Sun Oct 18 2020 10:01:30 GMT+0800 (中國標準時間)
Date.prototype.setMonth()
dateObj.setMonth(monthValue[, dayValue])
setMonth()
方法根據本地時間為一個設定年份的日期物件設定月份,返回從1970-01-01 00:00:00 UTC
到更新後的日期物件例項所表示時間的毫秒數,在JavaScript 1.3
版本之前只接受第一個引數。
monthValue
: 介於0
到11
之間的整數,表示一月到十二月。dayValue
: 從1
到31
之間的整數,表示月份中的第幾天,0
為上個月最後一天。
var date = new Date("2020-10-18 10:15:30");
date.setMonth(1);
console.log(date); // Tue Feb 18 2020 10:15:30 GMT+0800 (中國標準時間)
Date.prototype.setSeconds()
dateObj.setSeconds(secondsValue[, msValue])
setSeconds()
方法根據本地時間設定一個日期物件的秒數,在JavaScript 1.3
版本之前只接受第一個引數。
secondsValue
: 一個0
到59
的整數。msValue
: 一個0
到999
的數字,表示微秒數。
var date = new Date("2020-10-18 10:15:30");
date.setSeconds(1);
console.log(date); // Sun Oct 18 2020 10:15:01 GMT+0800 (中國標準時間)
Date.prototype.setTime()
dateObj.setTime(timeValue)
setTime()
方法以一個表示從1970-1-1 00:00:00 UTC
計時的毫秒數為來為Date
物件設定時間。
timeValue
: 一個整數,表示從1970-1-1 00:00:00 UTC
開始計時的毫秒數。
var date = new Date("2020-10-18 10:15:30");
date.setTime(1603011933306);
console.log(date); // Sun Oct 18 2020 17:05:33 GMT+0800 (中國標準時間)
Date.prototype.setUTCDate()
dateObj.setUTCDate(dayValue)
setUTCDate()
方法就是根據全球時間設定特定date
物件的日期。
dayValue
: 一個1-31
的整形數字,用來指定日期。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCDate(1);
console.log(date.toUTCString()); // Thu, 01 Oct 2020 10:15:30 GMT
Date.prototype.setUTCFullYear()
dateObj.setUTCFullYear(yearValue[, monthValue[, dayValue]])
setUTCFullYear()
方法根據世界標準時間為一個具體日期設定年份,在JavaScript 1.3
版本之前只接受第一個引數。
yearValue
: 指定年份整數值,例如1995
。monthValue
: 指定一個0-11
之間的整數值,代表從一月到十二月。dayValue
: 指定一個1-31
之間的整數值,代表月份中的第幾天,如果指定了dayValue
引數,那麼必須指定monthValue
引數。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCFullYear(2019);
console.log(date.toUTCString()); // Fri, 18 Oct 2019 10:15:30 GMT
Date.prototype.setUTCHours()
dateObj.setUTCHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
setUTCHours()
方法根據通用時間設定指定日期的小時,並返回從1970
年1
月1
日00:00:00 UTC
到更新後的date
例項所表示的時間的毫秒數。
hoursValue
: 表示小時的整數,取值0
到23
之間。minutesValue
: 表示分鐘的整數,取值0
到59
之間。secondsValue
: 表示秒數的整數,取值0
到59
之間,如果指定了該引數,就要同時指定minutesValue
引數。msValue
: 表示毫秒的整數,取值0
到999
之間,如果指定了該引數,就要指定minutesValue
和secondsValue
這兩個引數。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCHours(1);
console.log(date.toUTCString()); // Sun, 18 Oct 2020 01:15:30 GMT
Date.prototype.setUTCMilliseconds()
dateObj.setUTCMilliseconds(millisecondsValue)
setUTCMilliseconds()
方法會根據世界時來設定指定時間的毫秒數。
millisecondsValue
:0 - 999
之間的數值,代表毫秒數。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCMilliseconds(111);
console.log(date.getUTCMilliseconds()); // 111
Date.prototype.setUTCMinutes()
dateObj.setUTCMinutes(minutesValue[, secondsValue[, msValue]])
setUTCMinutes()
方法會根據世界協調時UTC
來設定指定日期的分鐘數,返回從UTC
時間1970
年1
月1
日0
時0
分0
秒至設定後的時間的毫秒數。
minutesValue
: 表示要設定的分鐘數,是一個介於0
和59
之間的整數。secondsValue
: 表示要設定的秒數,同樣也是一個介於0
和59
之間的整數,如果傳入了這個引數,那麼必須要傳入上一個引數minutesValue
。msValue
: 表示要設定的毫秒數,這是一個介於0
和999
之間的數字,如果傳入了這個引數,那麼就必須要傳入前面兩個引數minutesValue
和secondsValue
。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCMinutes(1);
console.log(date.toUTCString()); // Sun, 18 Oct 2020 10:01:30 GMT
Date.prototype.setUTCMonth()
dateObj.setUTCMonth(monthValue[, dayValue])
setUTCMonth()
方法根據通用的時間來設定一個準確的月份,返回從UTC
時間1970
年1
月1
日0
時0
分0
秒至設定後的時間的毫秒數。
monthValue
: 一個0-11
的整數,代表1
月到12
月。dayValue
: 一個1-31
的整數,代表一個月的天數。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCMonth(1);
console.log(date.toUTCString()); // Tue, 18 Feb 2020 10:15:30 GMT
Date.prototype.setUTCSeconds()
dateObj.setUTCSeconds(secondsValue[, msValue])
setUTCSeconds()
方法為一個依據國際通用時間的特定日期設定秒數,返回從UTC
時間1970
年1
月1
日0
時0
分0
秒至設定後的時間的毫秒數。
secondsValue
: 一個在0
到59
之間的整數,表示秒數。msValue
: 一個0
到999
之間的數字,代表毫秒數。
var date = new Date("2020-10-18 10:15:30 GMT");
date.setUTCSeconds(1);
console.log(date.toUTCString()); // Sun, 18 Oct 2020 10:15:01 GMT
Date.prototype.toDateString()
dateObj.toDateString()
toDateString()
方法返回一個日期物件日期部分的字串。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toDateString()); // Sun Oct 18 2020
Date.prototype.toISOString()
dateObj.toISOString()
toISOString()
方法返回一個ISO
即ISO 8601 Extended Format
格式的字串YYYY-MM-DDTHH:mm:ss.sssZ
,時區總是UTC
協調世界時,加一個字尾Z
標識。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toISOString()); // 2020-10-18T02:15:30.000Z
Date.prototype.toJSON()
dateObj.toJSON()
toJSON()
方法返回Date
物件的字串形式,呼叫toJSON()
返回一個JSON
格式字串,使用toISOString()
,表示該日期物件的值,預設情況下,這個方法常用於JSON
序列化Date
物件。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toJSON()); // 2020-10-18T02:15:30.000Z
Date.prototype.toLocaleDateString()
dateObj.toLocaleDateString([locales [, options]])
toLocaleDateString()
方法返回該日期物件日期部分的字串,該字串格式因不同語言而不同。新增的引數locales
和options
使程式能夠指定使用哪種語言格式化規則,允許定製該方法的表現behavior
,在舊版本瀏覽器中,locales
和options
引數被忽略,使用的語言環境和返回的字串格式是各自獨立實現的。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toLocaleDateString()); // 2020/10/18
Date.prototype.toLocaleString()
dateObj.toLocaleString([locales [, options]])
toLocaleString()
方法返回該日期物件的字串,該字串格式因不同語言而不同。新增的引數locales
和options
使程式能夠指定使用哪種語言格式化規則,允許定製該方法的表現behavior
。在舊版本瀏覽器中,locales
和options
引數被忽略,使用的語言環境和返回的字串格式是各自獨立實現的。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toLocaleString()); // 2020/10/18 上午10:15:30
Date.prototype.toLocaleTimeString()
dateObj.toLocaleTimeString([locales [, options]])
toLocaleTimeString()
方法返回該日期物件時間部分的字串,該字串格式因不同語言而不同。新增的引數locales
和options
使程式能夠指定使用哪種語言格式化規則,允許定製該方法的表現behavior
。在舊版本瀏覽器中,locales
和options
引數被忽略,使用的語言環境和返回的字串格式是各自獨立實現的。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toLocaleTimeString()); // 上午10:15:30
Date.prototype.toString()
dateObj.toString()
toString()
方法返回一個字串,表示該Date
物件。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toString()); // Sun Oct 18 2020 10:15:30 GMT+0800 (中國標準時間)
Date.prototype.toTimeString()
dateObj.toTimeString()
toTimeString()
方法返回一個日期物件時間部分的字串。
var date = new Date("2020-10-18 10:15:30");
console.log(date.toTimeString()); // 10:15:30 GMT+0800 (中國標準時間)
Date.prototype.toUTCString()
dateObj.toUTCString()
toUTCString()
方法把一個日期轉換為一個字串,使用UTC
時區。
var date = new Date("2020-10-18 10:15:30 GMT");
console.log(date.toUTCString()); // Sun, 18 Oct 2020 10:15:30 GMT
Date.prototype.valueOf()
dateObj.valueOf()
valueOf()
方法返回一個Date
物件的原始值。
var date = new Date("2020-10-18 10:15:30");
console.log(date.valueOf()); // 1602987330000
Date.prototype[@@toPrimitive]
Date()[Symbol.toPrimitive]()
[@@toPrimitive]()
方法可以轉換一個Date
物件到一個原始值。如果hint
是string
或default
,[@@toPrimitive]()
將會呼叫toString
,如果toString
屬性不存在,則呼叫 valueOf
,如果valueOf
也不存在,則丟擲一個TypeError
。如果hint
是number
,[@@toPrimitive]()
會首先嚐試valueOf
,若失敗再嘗試toString
。當期望一個原始值卻收到一個物件時,JavaScript
可以自動的呼叫[@@toPrimitive]()
方法來將一個物件轉化成原始值,所以你很少會需要自己呼叫這個方法。
var date = new Date("2020-10-18 10:15:30");
console.log(date[Symbol.toPrimitive]("string")); // Sun Oct 18 2020 10:15:30 GMT+0800 (中國標準時間)
每日一題
https://github.com/WindrunnerMax/EveryDay
參考
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date