Date
對象用於處理日期與時間。javascript
new Date(); new Date(value); new Date(dateString); new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
參數:html
value 表明自1970年1月1日00:00:00 (世界標準時間) 起通過的毫秒數。 dateString 表示日期的字符串值。該字符串應該能被 Date.parse() 方法識別(符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。 year 表明年份的整數值。爲了不2000年問題最好指定4位數的年份; 使用 1998, 而不要用 98. month 表明月份的整數值從0(1月)到11(12月)。 day 表明一個月中的第幾天的整數值,從1開始。 hour 表明一天中的小時數的整數值 (24小時制)。 minute 分鐘數。 second 秒數。 millisecond 表示時間的毫秒部分的整數值。
須要注意的是隻能經過調用 Date 構造函數來實例化日期對象:以常規函數調用它(即不加 new 操做符)將會返回一個字符串,而不是一個日期對象。另外,不像其餘JavaScript 類型,Date 對象沒有字面量格式。
當Date做爲構造函數調用並傳入多個參數時,若是數值大於合理範圍時(如月份爲13或者分鐘數爲70),相鄰的數值會被調整。好比 new Date(2013, 13, 1)等於new Date(2014, 1, 1),它們都表示日期2014-02-01(注意月份是從0開始的)。其餘數值也是相似,new Date(2013, 2, 1, 0, 70)等於new Date(2013, 2, 1, 1, 10),都表示時間2013-03-01T01:10:00。
表示日期的字符串值。該字符串應該能被Date.parse()
方法識別(符合IETF-compliant RFC 2822 timestamps
或version of ISO8601
)。因此須要確認datestring
的類型。
最初的datestring
的只支持:時間字符串java
Date and Time Specification Date and time occur in several header fields. This section specifies the syntax for a full date and time specification. Though folding white space is permitted throughout the date-time specification, it is RECOMMENDED that a single space be used in each place that FWS appears (whether it is required or optional); some older implementations may not interpret other occurrences of folding white space correctly. date-time = [ day-of-week "," ] date FWS time [CFWS] day-of-week = ([FWS] day-name) / obs-day-of-week day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun" date = day month year year = 4*DIGIT / obs-year month = (FWS month-name FWS) / obs-month month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec" day = ([FWS] 1*2DIGIT) / obs-day time = time-of-day FWS zone time-of-day = hour ":" minute [ ":" second ] hour = 2DIGIT / obs-hour minute = 2DIGIT / obs-minute second = 2DIGIT / obs-second zone = (( "+" / "-" ) 4DIGIT) / obs-zone The day is the numeric day of the month. The year is any numeric year 1900 or later. The time-of-day specifies the number of hours, minutes, and optionally seconds since midnight of the date indicated. The date and time-of-day SHOULD express local time. The zone specifies the offset from Coordinated Universal Time (UTC, formerly referred to as "Greenwich Mean Time") that the date and time-of-day represent. The "+" or "-" indicates whether the time-of-day is ahead of (i.e., east of) or behind (i.e., west of) Universal Time. The first two digits indicate the number of hours difference from Universal Time, and the last two digits indicate the number of minutes difference from Universal Time. (Hence, +hhmm means +(hh * 60 + mm) minutes, and -hhmm means -(hh * 60 + mm) minutes). The form "+0000" SHOULD be used to indicate a time zone at Universal Time. Though "-0000" also indicates Universal Time, it is used to indicate that the time was generated on a system that may be in a local time zone other than Universal Time and therefore indicates that the date-time contains no information about the local time zone. A date-time specification MUST be semantically valid. That is, the day-of-the-week (if included) MUST be the day implied by the date, the numeric day-of-month MUST be between 1 and the number of days allowed for the specified month (in the specified year), the time-of-day MUST be in the range 00:00:00 through 23:59:60 (the number of seconds allowing for a leap second; see [STD12]), and the zone MUST be within the range -9959 through +9959.
因此能夠得出結論:最開始的js僅支持如下格式的datestring【須要注意的是各個瀏覽器實現不一致,因此可能會支持其餘的格式的datestring
】git
Sun 30 Dec 2018 19:42:44 +0800
看起來和console.log
裏面的輸出有一點不一致;express
Sun Dec 30 2018 19:42:44 GMT+0800 (中國標準時間)
在es5開始,添加了ISO8601格式的支持;瀏覽器
Date Time String Format ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ Where the fields are as follows: YYYY is the decimal digits of the year 0000 to 9999 in the Gregorian calendar. - 「-」 (hyphen) appears literally twice in the string. MM is the month of the year from 01 (January) to 12 (December). DD is the day of the month from 01 to 31. T 「T」 appears literally in the string, to indicate the beginning of the time element. HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24. : 「:」 (colon) appears literally twice in the string. mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59. ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59. . 「.」 (dot) appears literally in the string. sss is the number of complete milliseconds since the start of the second as three decimal digits. Z is the time zone offset specified as 「Z」 (for UTC) or either 「+」 or 「-」 followed by a time expression HH:mm This format includes date-only forms: YYYY YYYY-MM YYYY-MM-DD It also includes 「date-time」 forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional time zone offset appended: THH:mm THH:mm:ss THH:mm:ss.sss All numbers must be base 10. If the MM or DD fields are absent 「01」 is used as the value. If the HH, mm, or ss fields are absent 「00」 is used as the value and the value of an absent sss field is 「000」. The value of an absent time zone offset is 「Z」. Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format. NOTE 1 As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time: 1995-02-04T24:00 and 1995-02-05T00:00 NOTE 2 There exists no international standard that specifies abbreviations for civil time zones like CET, EST, etc. and sometimes the same abbreviation is even used for two very different time zones. For this reason, ISO 8601 and this format specifies numeric representations of date and time.
datestring
的格式能夠是以下:app
其中[date]能夠是:函數
而[time]能夠是一下:ui
能夠說[date]是必須有的,其餘的都是能夠沒有,會取默認值,固然各個瀏覽器內部實現的不一致,因此可能還會出現不一致的問題;this
Date.UTC() Date.now() Date.parse() Date.prototype.getDate() Date.prototype.getDay() Date.prototype.getFullYear() Date.prototype.getHours() Date.prototype.getMilliseconds() Date.prototype.getMinutes() Date.prototype.getMonth() Date.prototype.getSeconds() Date.prototype.getTime() Date.prototype.getTimezoneOffset() Date.prototype.getUTCDate() Date.prototype.getUTCDay() Date.prototype.getUTCFullYear() Date.prototype.getUTCHours() Date.prototype.getUTCMilliseconds() Date.prototype.getUTCMinutes() Date.prototype.getUTCMonth() Date.prototype.getUTCSeconds() Date.prototype.getYear() Date.prototype.setDate() Date.prototype.setFullYear() Date.prototype.setHours() Date.prototype.setMilliseconds() Date.prototype.setMinutes() Date.prototype.setMonth() Date.prototype.setSeconds() Date.prototype.setTime() Date.prototype.setUTCDate() Date.prototype.setUTCFullYear() Date.prototype.setUTCHours() Date.prototype.setUTCMilliseconds() Date.prototype.setUTCMinutes() Date.prototype.setUTCMonth() Date.prototype.setUTCSeconds() Date.prototype.setYear() Date.prototype.toDateString() Date.prototype.toGMTString() Date.prototype.toISOString() Date.prototype.toJSON() Date.prototype.toLocaleDateString() Date.prototype.toLocaleFormat() Date.prototype.toLocaleString() Date.prototype.toLocaleTimeString() Date.prototype.toSource() Date.prototype.toString() Date.prototype.toTimeString() Date.prototype.toUTCString() Date.prototype.valueOf() Date.prototype[@@toPrimitive]
+new Date() // 1546335448125 0+new Date() // "0Tue Jan 01 2019 17:53:17 GMT+0800 (中國標準時間)"
上圖中的NOTE 1說,在步驟5和6中的ToPrimitive調用沒有提供hint,除了Date對象以外全部ECMAScript對象將缺乏hint的狀況當作Number處理;
而本文須要看的是date[@@toPrimitive]() 方法能夠轉換一個 Date 對象到一個原始值。
Date()[Symbol.toPrimitive](hint)
==>date.toPrimitive;
給出的 Date
的原始值。根據傳入參數的不一樣,能夠返回 string 或 number
類型。
Date 對象的 [@@toPrimitive]() 方法能夠返回一個原始值,或是 string,或是number
。
若是 hint
是 "string"
或 "default"
,[@@toPrimitive]()
將會調用 toString
。若是 toString
屬性不存在,則調用 valueOf
。若是 valueOf
也不存在,則拋出一個TypeError。
若是 hint
是 "number"
,[@@toPrimitive]()
會首先嚐試 valueOf
,若失敗再嘗試 toString
。
當指望一個原始值卻收到一個對象時,JavaScript 能夠自動的調用 [@@toPrimitive]()
方法來將一個對象轉化成原始值,因此你不多會須要本身調用這個方法。
date的toPrimitive
調用,不傳遞hint的時候,默認是string
;
因此:
0+new Date() // "0Tue Jan 01 2019 17:53:17 GMT+0800 (中國標準時間)"
'+'號運算符做爲一元運算符時,Expression將進行ToNumber操做。
能夠看到,此時date的toPrimitive調用,傳遞hint是number,返回毫秒數;