21.4.1 Date 对象概述以及抽象操作定义
以下抽象操作 作用于时间值 (定义见 21.4.1.1 )。注意,在每种情况下,如果这些函数之一的任何实参是 NaN ,则结果将是 NaN 。
21.4.1.1 时间值和时间范围
ECMAScript 中的时间度量类似于 POSIX 中的时间度量,特别是共享以预推格里高利历、UTC 1970 年 1 月 1 日开始时的午夜作为 epoch ,并且把每一天都计为正好 86,400 秒(每秒长度为 1000 毫秒)的定义。
ECMAScript 时间值 是 Number,要么是表示毫秒精度的时间瞬间的有限 整数 Number,要么是表示没有特定瞬间的 NaN 。作为 24 × 60 × 60 × 1000 = 86,400,000 的倍数的时间值(即,对于某个整数 d ,是 86,400,000 × d )表示从 epoch 起经过 d 个完整 UTC 日后的 UTC 日开始处的瞬间(负 d 表示早于 epoch )。其他每个有限 时间值 tv 都相对于此前最大的、满足这种倍数条件的时间值 s 来定义,并表示发生在与 s 同一个 UTC 日内、但在其后 (tv - s ) 毫秒的瞬间。
时间值不考虑 UTC 闰秒——没有表示正闰秒内瞬间的时间值,并且存在表示因负闰秒而从 UTC 时间线移除的瞬间的时间值。不过,时间值的定义仍然产生与 UTC 的分段对齐,仅在闰秒边界处存在不连续,并且在闰秒之外差值为零。
Number 可以精确表示从 -9,007,199,254,740,992 到 9,007,199,254,740,992 的所有整数(21.1.2.8 和 21.1.2.6 )。时间值支持略小的范围,即 -8,640,000,000,000,000 到 8,640,000,000,000,000 毫秒。这产生的受支持时间值范围,相对于 UTC 1970 年 1 月 1 日开始时的午夜,正好是 -100,000,000 天到 100,000,000 天。
UTC 1970 年 1 月 1 日开始时午夜的精确时刻由时间值 +0 𝔽 表示。
Note
在预推格里高利历中,闰年正是那些可被 4 整除并且可被 400 整除或不能被 100 整除的年份。
预推格里高利历的 400 年周期包含 97 个闰年。这产生每年平均 365.2425 天,即 31,556,952,000 毫秒。因此,Number 能以毫秒精度精确表示的最大范围,相对于 1970 年大约是 -285,426 年到 285,426 年。本节规定的时间值所支持的较小范围,相对于 1970 年大约是 -273,790 年到 273,790 年。
21.4.1.2 时间相关常量
以下各节中的算法会引用这些常量。
HoursPerDay = 24
MinutesPerHour = 60
SecondsPerMinute = 60
msPerSecond = 1000 𝔽
21.4.1.3 Day ( tv )
The abstract operation Day takes argument tv (a finite time value) and returns an integral Number . 它返回 tv 所落入日期的日编号。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (tv / msPerDay )))。
21.4.1.4 TimeWithinDay ( tv )
The abstract operation TimeWithinDay takes argument tv (a finite time value) and returns an integral Number in the interval from +0 𝔽 (inclusive) to msPerDay (exclusive). 它返回自 tv 所落入日期开始以来的毫秒数。 It performs the following steps when called:
返回 𝔽 (ℝ (tv ) modulo ℝ (msPerDay ))。
21.4.1.5 DaysInYear ( y )
The abstract operation DaysInYear takes argument y (an integral Number ) and returns 365 𝔽 or 366 𝔽 . 它返回年份 y 中的天数。闰年有 366 天;所有其他年份有 365 天。 It performs the following steps when called:
令 ry 为 ℝ (y )。 如果 (ry modulo 400) = 0,则返回 366 𝔽 。 如果 (ry modulo 100) = 0,则返回 365 𝔽 。 如果 (ry modulo 4) = 0,则返回 366 𝔽 。 返回 365 𝔽 。
21.4.1.6 DayFromYear ( y )
The abstract operation DayFromYear takes argument y (an integral Number ) and returns an integral Number . 它返回年份 y 第一天的日编号。 It performs the following steps when called:
令 ry 为 ℝ (y )。 注:在以下步骤中,numberYears1 、numberYears4 、numberYears100 和 numberYears400 分别表示在 epoch 与年份 y 开始之间出现的、可被 1、4、100 和 400 整除的年份数。如果 y 早于 epoch ,则该数为负。 令 numberYears1 为 (ry - 1970)。 令 numberYears4 为 floor ((ry - 1969) / 4)。 令 numberYears100 为 floor ((ry - 1901) / 100)。 令 numberYears400 为 floor ((ry - 1601) / 400)。 返回 𝔽 (365 × numberYears1 + numberYears4 - numberYears100 + numberYears400 )。
21.4.1.7 TimeFromYear ( y )
The abstract operation TimeFromYear takes argument y (an integral Number ) and returns a time value. 它返回年份 y 开始处的时间值 。 It performs the following steps when called:
返回 msPerDay × DayFromYear (y )。
21.4.1.8 YearFromTime ( tv )
The abstract operation YearFromTime takes argument tv (a finite time value) and returns an integral Number . 它返回 tv 所落入的年份。 It performs the following steps when called:
返回最大的整数 Number y (最接近 +∞),使得 TimeFromYear (y ) ≤ tv 。
21.4.1.9 DayWithinYear ( tv )
The abstract operation DayWithinYear takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 365 𝔽 . It performs the following steps when called:
返回 Day (tv ) - DayFromYear (YearFromTime (tv ))。
21.4.1.10 InLeapYear ( tv )
The abstract operation InLeapYear takes argument tv (a finite time value) and returns +0 𝔽 or 1 𝔽 . 如果 tv 位于闰年内,则返回 1 𝔽 ;否则返回 +0 𝔽 。 It performs the following steps when called:
如果 DaysInYear (YearFromTime (tv )) 是 366 𝔽 ,则返回 1 𝔽 。 返回 +0 𝔽 。
21.4.1.11 MonthFromTime ( tv )
The abstract operation MonthFromTime takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 11 𝔽 . 它返回一个 Number,用于标识 tv 所落入的月份。月份值 +0 𝔽 指定一月;1 𝔽 指定二月;2 𝔽 指定三月;3 𝔽 指定四月;4 𝔽 指定五月;5 𝔽 指定六月;6 𝔽 指定七月;7 𝔽 指定八月;8 𝔽 指定九月;9 𝔽 指定十月;10 𝔽 指定十一月;11 𝔽 指定十二月。注意,MonthFromTime(+0 𝔽 ) = +0 𝔽 ,对应于 1970 年 1 月 1 日星期四。 It performs the following steps when called:
令 inLeapYear 为 InLeapYear (tv )。 令 dayWithinYear 为 DayWithinYear (tv )。 如果 dayWithinYear < 31 𝔽 ,则返回 +0 𝔽 。 如果 dayWithinYear < 59 𝔽 + inLeapYear ,则返回 1 𝔽 。 如果 dayWithinYear < 90 𝔽 + inLeapYear ,则返回 2 𝔽 。 如果 dayWithinYear < 120 𝔽 + inLeapYear ,则返回 3 𝔽 。 如果 dayWithinYear < 151 𝔽 + inLeapYear ,则返回 4 𝔽 。 如果 dayWithinYear < 181 𝔽 + inLeapYear ,则返回 5 𝔽 。 如果 dayWithinYear < 212 𝔽 + inLeapYear ,则返回 6 𝔽 。 如果 dayWithinYear < 243 𝔽 + inLeapYear ,则返回 7 𝔽 。 如果 dayWithinYear < 273 𝔽 + inLeapYear ,则返回 8 𝔽 。 如果 dayWithinYear < 304 𝔽 + inLeapYear ,则返回 9 𝔽 。 如果 dayWithinYear < 334 𝔽 + inLeapYear ,则返回 10 𝔽 。 断言:dayWithinYear < 365 𝔽 + inLeapYear 。 返回 11 𝔽 。
21.4.1.12 DateFromTime ( tv )
The abstract operation DateFromTime takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from 1 𝔽 to 31 𝔽 . 它返回 tv 所落入月份中的日期。 It performs the following steps when called:
令 inLeapYear 为 InLeapYear (tv )。 令 dayWithinYear 为 DayWithinYear (tv )。 令 month 为 MonthFromTime (tv )。 如果 month 是 +0 𝔽 ,则返回 dayWithinYear + 1 𝔽 。 如果 month 是 1 𝔽 ,则返回 dayWithinYear - 30 𝔽 。 如果 month 是 2 𝔽 ,则返回 dayWithinYear - 58 𝔽 - inLeapYear 。 如果 month 是 3 𝔽 ,则返回 dayWithinYear - 89 𝔽 - inLeapYear 。 如果 month 是 4 𝔽 ,则返回 dayWithinYear - 119 𝔽 - inLeapYear 。 如果 month 是 5 𝔽 ,则返回 dayWithinYear - 150 𝔽 - inLeapYear 。 如果 month 是 6 𝔽 ,则返回 dayWithinYear - 180 𝔽 - inLeapYear 。 如果 month 是 7 𝔽 ,则返回 dayWithinYear - 211 𝔽 - inLeapYear 。 如果 month 是 8 𝔽 ,则返回 dayWithinYear - 242 𝔽 - inLeapYear 。 如果 month 是 9 𝔽 ,则返回 dayWithinYear - 272 𝔽 - inLeapYear 。 如果 month 是 10 𝔽 ,则返回 dayWithinYear - 303 𝔽 - inLeapYear 。 断言:month 是 11 𝔽 。 返回 dayWithinYear - 333 𝔽 - inLeapYear 。
21.4.1.13 WeekDay ( tv )
The abstract operation WeekDay takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 6 𝔽 . 它返回一个 Number,用于标识 tv 所落入的星期几。星期值 +0 𝔽 指定星期日;1 𝔽 指定星期一;2 𝔽 指定星期二;3 𝔽 指定星期三;4 𝔽 指定星期四;5 𝔽 指定星期五;6 𝔽 指定星期六。注意,WeekDay(+0 𝔽 ) = 4 𝔽 ,对应于 1970 年 1 月 1 日星期四。 It performs the following steps when called:
返回 𝔽 (ℝ (Day (tv ) + 4 𝔽 ) modulo 7)。
21.4.1.14 HourFromTime ( tv )
The abstract operation HourFromTime takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 23 𝔽 . 它返回 tv 所落入日期中的小时。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (tv / msPerHour )) modulo HoursPerDay )。
21.4.1.15 MinFromTime ( tv )
The abstract operation MinFromTime takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 59 𝔽 . 它返回 tv 所落入小时中的分钟。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (tv / msPerMinute )) modulo MinutesPerHour )。
21.4.1.16 SecFromTime ( tv )
The abstract operation SecFromTime takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 59 𝔽 . 它返回 tv 所落入分钟中的秒。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (tv / msPerSecond )) modulo SecondsPerMinute )。
21.4.1.17 msFromTime ( tv )
The abstract operation msFromTime takes argument tv (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 999 𝔽 . 它返回 tv 所落入秒中的毫秒。 It performs the following steps when called:
返回 𝔽 (ℝ (tv ) modulo ℝ (msPerSecond ))。
21.4.1.18 GetUTCEpochNanoseconds ( year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )
The abstract operation GetUTCEpochNanoseconds takes arguments year (an integer ), month (an integer in the inclusive interval from 1 to 12), day (an integer in the inclusive interval from 1 to 31), hour (an integer in the inclusive interval from 0 to 23), minute (an integer in the inclusive interval from 0 to 59), second (an integer in the inclusive interval from 0 to 59), millisecond (an integer in the inclusive interval from 0 to 999), microsecond (an integer in the inclusive interval from 0 to 999), and nanosecond (an integer in the inclusive interval from 0 to 999) and returns a BigInt. 返回值表示自 epoch 以来的纳秒数,该值对应于给定的 ISO 8601 日历日期和 UTC 挂钟时间。 It performs the following steps when called:
令 date 为 MakeDay (𝔽 (year ), 𝔽 (month - 1), 𝔽 (day ))。 令 time 为 MakeTime (𝔽 (hour ), 𝔽 (minute ), 𝔽 (second ), 𝔽 (millisecond ))。 令 ms 为 MakeDate (date , time )。 断言:ms 是整数 Number。 返回 ℤ (ℝ (ms ) × 106 + microsecond × 103 + nanosecond )。
21.4.1.19 时区标识符
ECMAScript 中的时区由时区标识符 表示,它们是完全由从 0x0000 到 0x007F 闭区间 内的码元组成的 String。
ECMAScript 实现支持的时区可以是可用命名时区 ,由 AvailableNamedTimeZoneIdentifiers 返回的 Time Zone Identifier Records 的 [[Identifier]] 字段表示;也可以是偏移时区 ,由 IsTimeZoneOffsetString 返回 true 的 String 表示。
主时区标识符 是某个可用命名时区的首选标识符。
非主时区标识符 是某个可用命名时区的标识符,且不是主时区标识符。
可用命名时区标识符 要么是主时区标识符,要么是非主时区标识符。
每个可用命名时区标识符都恰好关联到一个可用命名时区。
每个可用命名时区都恰好关联到一个主时区标识符,并关联到零个或更多非主时区标识符。
ECMAScript 实现必须支持标识符为 "UTC" 的可用命名时区,该标识符必须是 UTC 时区的主时区标识符。
此外,实现可以支持任意数量的其他可用命名时区。
遵循 ECMA-402 国际化 API 规范中所述时区要求的实现称为时区感知 。
时区感知实现必须支持与 IANA Time Zone Database 的 Zone 和 Link 名称对应的可用命名时区,并且只能支持这样的名称。
在时区感知实现中,主时区标识符分别是 IANA Time Zone Database 中的 Zone 名称,非主时区标识符是 Link 名称,但 ECMA-402 规范中规定的 AvailableNamedTimeZoneIdentifiers 特别覆盖的 情况除外。
仍建议不支持完整 IANA Time Zone Database 的实现使用 IANA Time Zone Database 名称作为表示时区的标识符。
21.4.1.20 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier , year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )
The implementation-defined abstract operation GetNamedTimeZoneEpochNanoseconds takes arguments timeZoneIdentifier (a String), year (an integer ), month (an integer in the inclusive interval from 1 to 12), day (an integer in the inclusive interval from 1 to 31), hour (an integer in the inclusive interval from 0 to 23), minute (an integer in the inclusive interval from 0 to 59), second (an integer in the inclusive interval from 0 to 59), millisecond (an integer in the inclusive interval from 0 to 999), microsecond (an integer in the inclusive interval from 0 to 999), and nanosecond (an integer in the inclusive interval from 0 to 999) and returns a List of BigInts.
返回的 List 中的每个值都表示自 epoch 以来的纳秒数,该值对应于由 timeZoneIdentifier 标识的命名时区中的给定 ISO 8601 日历日期和挂钟时间。
当输入表示的本地时间因负时区转换而出现多次(例如夏令时结束时,或因时区规则变更而时区偏移减少时),返回的 List 将有多个元素,并按数值升序排序。
当输入表示的本地时间因正时区转换而被跳过(例如夏令时开始时,或因时区规则变更而时区偏移增加时),返回的 List 将为空。
否则,返回的 List 将有一个元素。
GetNamedTimeZoneEpochNanoseconds 的默认实现(用于不包含 任何时区本地政治规则的 ECMAScript 实现)在被调用时执行以下步骤:
断言:timeZoneIdentifier 是 "UTC" 。 令 epochNanoseconds 为 GetUTCEpochNanoseconds (year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )。 返回 « epochNanoseconds »。
Note
要求时区感知 实现(并建议所有其他实现)使用 IANA Time Zone Database 的时区信息 https://www.iana.org/time-zones/ 。
America/New_York 中 2017 年 11 月 5 日凌晨 1:30 会重复两次,因此 GetNamedTimeZoneEpochNanoseconds("America/New_York" , 2017, 11, 5, 1, 30, 0, 0, 0, 0) 会返回一个长度为 2 的 List ,其中第一个元素表示 05:30 UTC(对应 UTC 偏移 -04:00 的美国东部夏令时 01:30),第二个元素表示 06:30 UTC(对应 UTC 偏移 -05:00 的美国东部标准时 01:30)。
America/New_York 中 2017 年 3 月 12 日凌晨 2:30 不存在,因此 GetNamedTimeZoneEpochNanoseconds("America/New_York" , 2017, 3, 12, 2, 30, 0, 0, 0, 0) 会返回一个空 List 。
21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier , epochNanoseconds )
The implementation-defined abstract operation GetNamedTimeZoneOffsetNanoseconds takes arguments timeZoneIdentifier (a String) and epochNanoseconds (a BigInt) and returns an integer .
返回的整数表示由 timeZoneIdentifier 标识的命名时区,在相对于 epoch 与 epochNanoseconds 对应的瞬间相对于 UTC 的偏移,两者都以纳秒为单位。
GetNamedTimeZoneOffsetNanoseconds 的默认实现(用于不包含 任何时区本地政治规则的 ECMAScript 实现)在被调用时执行以下步骤:
断言:timeZoneIdentifier 是 "UTC" 。 返回 0。
Note
21.4.1.22 Time Zone Identifier Record
Time Zone Identifier Record 是一种 Record ,用于描述可用命名时区标识符 及其对应的主时区标识符 。
Time Zone Identifier Record 具有 Table 60 中列出的字段。
Table 60: Time Zone Identifier Record 字段
字段名
值
含义
[[Identifier]]
a String
实现支持的可用命名时区标识符 。
[[PrimaryIdentifier]]
a String
[[Identifier]] 解析到的主时区标识符 。
Note
如果 [[Identifier]] 是主时区标识符 ,则 [[Identifier]] 就是 [[PrimaryIdentifier]] 。
21.4.1.23 AvailableNamedTimeZoneIdentifiers ( )
The implementation-defined abstract operation AvailableNamedTimeZoneIdentifiers takes no arguments and returns a List of Time Zone Identifier Records .
其结果描述此实现中的所有可用命名时区标识符 ,以及每个可用命名时区标识符 对应的主时区标识符 。
该 List 按每个 Time Zone Identifier Record 的 [[Identifier]] 字段排序。
时区感知 实现,包括所有实现 ECMA-402 国际化 API 的实现,必须按 ECMA-402 规范中的规定实现 AvailableNamedTimeZoneIdentifiers 抽象操作 。
对于非时区感知 实现,AvailableNamedTimeZoneIdentifiers 在被调用时执行以下步骤:
如果实现不包含 任何时区的本地政治规则,则返回 « Time Zone Identifier Record { [[Identifier]] : "UTC" , [[PrimaryIdentifier]] : "UTC" } »。 令 identifiers 为唯一可用命名时区标识符 的 List ,按字典序码元顺序 排序。 令 result 为新的空 List 。 对 identifiers 的每个元素 identifier ,执行:令 primary 为 identifier 。 如果 identifier 是此实现中的非主时区标识符 ,且 identifier 不是 "UTC" ,则将 primary 设置为与 identifier 关联的主时区标识符 。 注:实现可能需要迭代解析 identifier ,以获得主时区标识符 。 令 record 为 Time Zone Identifier Record { [[Identifier]] : identifier , [[PrimaryIdentifier]] : primary }。 将 record 追加到 result 。 断言:result 包含 一个 Time Zone Identifier Record record ,使得 record .[[Identifier]] 是 "UTC" 且 record .[[PrimaryIdentifier]] 是 "UTC" 。 返回 result 。
21.4.1.24 SystemTimeZoneIdentifier ( )
The implementation-defined abstract operation SystemTimeZoneIdentifier takes no arguments and returns a String.
它返回一个表示宿主环境 当前时区的 String,该 String 要么是表示 UTC 偏移且使 IsTimeZoneOffsetString 返回 true 的 String,要么是主时区标识符 。
It performs the following steps when called:
如果实现只支持 UTC 时区,则返回 "UTC" 。 令 systemTimeZoneString 为表示宿主环境 当前时区的 String,它要么是主时区标识符 ,要么是偏移时区 标识符。 返回 systemTimeZoneString 。
Note
为确保实现通常在 Date 对象方法中提供的功能水平,建议 SystemTimeZoneIdentifier 返回与宿主环境 时区设置对应的 IANA 时区名称,如果存在这样的名称。
如果该时区存在标准时和夏令时的本地政治规则,则 GetNamedTimeZoneEpochNanoseconds 和 GetNamedTimeZoneOffsetNanoseconds 必须反映这些规则。
例如,如果宿主环境 是在用户已选择美国东部时间作为其时区的系统上的浏览器,则 SystemTimeZoneIdentifier 返回 "America/New_York" 。
21.4.1.25 LocalTime ( tv )
The abstract operation LocalTime takes argument tv (a finite time value) and returns an integral Number .
它将 tv 从 UTC 转换为本地时间。
应使用 tv 处生效的标准时和夏令时本地政治规则,按本节规定的方式确定结果。
It performs the following steps when called:
令 systemTimeZoneIdentifier 为 SystemTimeZoneIdentifier ()。 如果 IsTimeZoneOffsetString (systemTimeZoneIdentifier ) 是 true ,则令 offsetNs 为 ParseTimeZoneOffsetString (systemTimeZoneIdentifier )。 否则,令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , ℤ (ℝ (tv ) × 106 ))。 令 offsetMs 为 truncate (offsetNs / 106 )。 返回 tv + 𝔽 (offsetMs )。
Note 1
Note 2
要求时区感知 实现(并建议所有其他实现)使用 IANA Time Zone Database 的时区信息 https://www.iana.org/time-zones/ 。
Note 3
两个不同的输入时间值 tv UTC 会在负时区转换处存在重复时间时(例如夏令时结束或时区调整减少时)转换为相同本地时间 tlocal 。
LocalTime(UTC (tv local )) 不一定总是等于 tv local 。相应地,UTC (LocalTime(tv UTC )) 也不一定总是等于 tv UTC 。
21.4.1.26 UTC ( t )
The abstract operation UTC takes argument t (a Number) and returns a time value.
它将 t 从本地时间转换为 UTC 时间值 。
应使用 t 处生效的标准时和夏令时本地政治规则,按本节规定的方式确定结果。
It performs the following steps when called:
如果 t 不是有限 的,则返回 NaN 。 令 systemTimeZoneIdentifier 为 SystemTimeZoneIdentifier ()。 如果 IsTimeZoneOffsetString (systemTimeZoneIdentifier ) 是 true ,则令 offsetNs 为 ParseTimeZoneOffsetString (systemTimeZoneIdentifier )。 否则,令 possibleInstants 为 GetNamedTimeZoneEpochNanoseconds (systemTimeZoneIdentifier , ℝ (YearFromTime (t )), ℝ (MonthFromTime (t )) + 1, ℝ (DateFromTime (t )), ℝ (HourFromTime (t )), ℝ (MinFromTime (t )), ℝ (SecFromTime (t )), ℝ (msFromTime (t )), 0, 0)。 注:以下步骤确保当 t 表示在负时区转换处重复多次的本地时间(例如夏令时结束时,或因时区规则变更而时区偏移减少时)或在正时区转换处被跳过的本地时间(例如夏令时开始时,或因时区规则变更而时区偏移增加时)时,t 会使用转换之前的时区偏移来解释。 如果 possibleInstants 非空,则令 disambiguatedInstant 为 possibleInstants [0]。 否则,注:t 表示在正时区转换处被跳过的本地时间(例如由于夏令时开始或时区规则变更增加 UTC 偏移)。 令 possibleInstantsBefore 为 GetNamedTimeZoneEpochNanoseconds (systemTimeZoneIdentifier , ℝ (YearFromTime (tBefore )), ℝ (MonthFromTime (tBefore )) + 1, ℝ (DateFromTime (tBefore )), ℝ (HourFromTime (tBefore )), ℝ (MinFromTime (tBefore )), ℝ (SecFromTime (tBefore )), ℝ (msFromTime (tBefore )), 0, 0),其中 tBefore 是小于 t 且使 possibleInstantsBefore 非空的最大整数 Number(即,tBefore 表示转换前的最后一个本地时间)。 令 disambiguatedInstant 为 possibleInstantsBefore 的最后一个元素。 令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , disambiguatedInstant )。 令 offsetMs 为 truncate (offsetNs / 106 )。 返回 t - 𝔽 (offsetMs )。
输入 t 名义上是时间值 ,但可以是任何 Number 值 。
算法不得将 t 限制在时间值 范围内,以便无论本地 UTC 偏移如何,都能支持与时间值 范围边界对应的输入。
例如,最大时间值 是 8.64 × 1015 ,对应于 "+275760-09-13T00:00:00Z" 。
在某个环境中,如果本地时区偏移在该瞬间比 UTC 早 1 小时,则它由更大的输入 8.64 × 1015 + 3.6 × 106 表示,对应于 "+275760-09-13T01:00:00+01:00" 。
如果实现内没有本地时间 t 的政治规则,则结果为 t ,因为 SystemTimeZoneIdentifier 返回 "UTC" 且 GetNamedTimeZoneOffsetNanoseconds 返回 0。
Note 1
要求时区感知 实现(并建议所有其他实现)使用 IANA Time Zone Database 的时区信息 https://www.iana.org/time-zones/ 。
America/New_York 中 2017 年 11 月 5 日凌晨 1:30 会重复两次(向后回退),但它必须被解释为 UTC-04 的凌晨 1:30,而不是 UTC-05 的凌晨 1:30。
在 UTC(TimeClip (MakeDate (MakeDay (2017, 10, 5), MakeTime (1, 30, 0, 0)))) 中,offsetMs 的值是 -4 × msPerHour 。
America/New_York 中 2017 年 3 月 12 日凌晨 2:30 不存在,但它必须被解释为 UTC-05 的凌晨 2:30(等价于 UTC-04 的凌晨 3:30)。
在 UTC(TimeClip (MakeDate (MakeDay (2017, 2, 12), MakeTime (2, 30, 0, 0)))) 中,offsetMs 的值是 -5 × msPerHour 。
Note 2
UTC(LocalTime (t UTC )) 不一定总是等于 t UTC 。相应地,LocalTime (UTC(t local )) 也不一定总是等于 t local 。
21.4.1.27 MakeTime ( hour , min , sec , ms )
The abstract operation MakeTime takes arguments hour (a Number), min (a Number), sec (a Number), and ms (a Number) and returns a Number. 它计算毫秒数。 It performs the following steps when called:
如果 hour 不是有限 的、min 不是有限 的、sec 不是有限 的,或 ms 不是有限 的,则返回 NaN 。 令 h 为 𝔽 (! ToIntegerOrInfinity (hour ))。 令 m 为 𝔽 (! ToIntegerOrInfinity (min ))。 令 s 为 𝔽 (! ToIntegerOrInfinity (sec ))。 令 milli 为 𝔽 (! ToIntegerOrInfinity (ms ))。 返回 ((h × msPerHour + m × msPerMinute ) + s × msPerSecond ) + milli 。
Note
MakeTime 中的算术是浮点算术,不满足结合律,因此操作必须按正确顺序执行。
21.4.1.28 MakeDay ( year , month , date )
The abstract operation MakeDay takes arguments year (a Number), month (a Number), and date (a Number) and returns a finite Number or NaN . 它计算天数。 It performs the following steps when called:
如果 year 不是有限 的、month 不是有限 的,或 date 不是有限 的,则返回 NaN 。 令 y 为 𝔽 (! ToIntegerOrInfinity (year ))。 令 m 为 𝔽 (! ToIntegerOrInfinity (month ))。 令 dt 为 𝔽 (! ToIntegerOrInfinity (date ))。 令 ym 为 y + 𝔽 (floor (ℝ (m ) / 12))。 如果 ym 不是有限 的,则返回 NaN 。 令 mn 为 𝔽 (ℝ (m ) modulo 12)。 找到一个有限 时间值 tv ,使得 YearFromTime (tv ) 是 ym ,MonthFromTime (tv ) 是 mn ,并且 DateFromTime (tv ) 是 1 𝔽 ;但如果这不可能(因为某个实参超出范围),则返回 NaN 。 返回 Day (tv ) + dt - 1 𝔽 。
21.4.1.29 MakeDate ( day , time )
The abstract operation MakeDate takes arguments day (a Number) and time (a Number) and returns a finite Number or NaN . 它计算毫秒数。 It performs the following steps when called:
如果 day 不是有限 的或 time 不是有限 的,则返回 NaN 。 令 tv 为 day × msPerDay + time 。 如果 tv 不是有限 的,则返回 NaN 。 返回 tv 。
21.4.1.30 MakeFullYear ( year )
The abstract operation MakeFullYear takes argument year (a Number) and returns an integral Number or NaN . 它返回与 year 的整数部分关联的完整年份,将从 0 到 99 的闭区间 内的任何值解释为自 1900 年开始以来的年数。为与预推格里高利历对齐,“完整年份”定义为自年份 0(公元前 1 年)开始以来的完整年数的带符号计数。 It performs the following steps when called:
如果 year 是 NaN 、+∞ 𝔽 或 -∞ 𝔽 之一,则返回 NaN 。 令 truncated 为 ! ToIntegerOrInfinity (year )。 如果 truncated 在从 0 到 99 的闭区间 内,则返回 1900 𝔽 + 𝔽 (truncated )。 返回 𝔽 (truncated )。
21.4.1.31 TimeClip ( time )
The abstract operation TimeClip takes argument time (a Number) and returns a time value. 它计算毫秒数。 It performs the following steps when called:
如果 time 不是有限 的,则返回 NaN 。 如果 abs (ℝ (time )) > 8.64 × 1015 ,则返回 NaN 。 返回 𝔽 (! ToIntegerOrInfinity (time ))。
21.4.1.32 日期时间字符串格式
ECMAScript 定义了一种基于 ISO 8601 日历日期扩展格式的简化形式的日期时间字符串交换格式。格式如下:YYYY-MM-DDTHH:mm:ss.sssZ
其中各元素如下:
YYYY
是预推格里高利历中的年份,表示为从 0000 到 9999 的四位十进制数字,或表示为一个扩展年份 ,即 "+" 或 "-" 后跟六位十进制数字。
-
"-" (连字符)在字符串中按字面出现两次。
MM
是一年中的月份,表示为从 01(一月)到 12(十二月)的两位十进制数字。
DD
是月份中的日期,表示为从 01 到 31 的两位十进制数字。
T
"T" 在字符串中按字面出现,用于指示时间元素的开始。
HH
是自午夜以来已经过去的完整小时数,表示为从 00 到 24 的两位十进制数字。
:
":" (冒号)在字符串中按字面出现两次。
mm
是自小时开始以来的完整分钟数,表示为从 00 到 59 的两位十进制数字。
ss
是自分钟开始以来的完整秒数,表示为从 00 到 59 的两位十进制数字。
.
"." (点)在字符串中按字面出现。
sss
是自秒开始以来的完整毫秒数,表示为三位十进制数字。
Z
是 UTC 偏移表示,指定为 "Z" (表示无偏移的 UTC),或指定为 "+" 或 "-" 后跟时间表达式 HH:mm(时区偏移字符串格式 的一个子集,分别表示本地时间早于或晚于 UTC)
此格式包括仅日期形式:
YYYY
YYYY-MM
YYYY-MM-DD
它还包括“日期时间”形式,该形式由上述某个仅日期形式立即后跟以下某个时间形式组成,并可选择附加 UTC 偏移表示:
THH:mm
THH:mm:ss
THH:mm:ss.sss
包含 越界元素或不符合格式元素的字符串,不是此格式的有效实例。
Note 1
由于每天都以午夜开始并以午夜结束,记法 00:00 和 24:00 可用于区分可与一个日期关联的两个午夜。这意味着以下两个记法指代完全相同的时间点:1995-02-04T24:00 和 1995-02-05T00:00。后一种形式作为“日历日结束”的这种解释与 ISO 8601 一致,尽管该规范将其保留用于描述时间区间 ,并且不允许在单个时间点的表示中使用它。
Note 2
不存在指定 CET、EST 等民用时区缩写的国际标准,并且有时同一个缩写甚至会用于两个非常不同的时区。因此,ISO 8601 和此格式都指定时区偏移的数字表示。
21.4.1.32.1 扩展年份
覆盖相对于 1970 年 1 月 1 日向前或向后约 273,790 年的完整时间值 范围(21.4.1.1 ),需要表示 0 年之前或 9999 年之后的年份。ISO 8601 允许扩展年份表示,但仅限于信息交换伙伴之间相互约定。在简化的 ECMAScript 格式中,这种扩展年份表示应具有 6 位数字,并且始终以前缀 + 或 - 符号开头。年份 0 被视为正数,并且必须以 + 符号为前缀。将年份 0 表示为 -000000 是无效的。与日期时间字符串格式 匹配、且扩展年份表示的时间瞬间超出时间值 范围的字符串,会被 Date.parse 视为不可识别,并使该函数返回 NaN ,且不会回退到实现特定行为或启发式规则。
Note
带扩展年份的 date-time values 示例:
-271821-04-20T00:00:00Z
公元前 271822 年
-000001-01-01T00:00:00Z
公元前 2 年
+000000-01-01T00:00:00Z
公元前 1 年
+000001-01-01T00:00:00Z
公元 1 年
+001970-01-01T00:00:00Z
公元 1970 年
+002009-12-15T00:00:00Z
公元 2009 年
+275760-09-13T00:00:00Z
公元 275760 年
21.4.1.33 时区偏移字符串格式
ECMAScript 定义了一种派生自 ISO 8601 的 UTC 偏移字符串交换格式。
该格式由以下文法描述。
语法
UTCOffset :::
ASCIISign
Hour
ASCIISign
Hour
HourSubcomponents [+Extended]
ASCIISign
Hour
HourSubcomponents [~Extended]
ASCIISign ::: one of + -
Hour :::
0
DecimalDigit
1
DecimalDigit
20
21
22
23
HourSubcomponents [Extended] :::
TimeSeparator [?Extended]
MinuteSecond
TimeSeparator [?Extended]
MinuteSecond
TimeSeparator [?Extended]
MinuteSecond
TemporalDecimalFraction opt
TimeSeparator [Extended] ::: [+Extended]
:
[~Extended]
[empty]
MinuteSecond :::
0
DecimalDigit
1
DecimalDigit
2
DecimalDigit
3
DecimalDigit
4
DecimalDigit
5
DecimalDigit
TemporalDecimalFraction :::
TemporalDecimalSeparator
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
DecimalDigit
TemporalDecimalSeparator ::: one of . ,
21.4.1.33.1 IsTimeZoneOffsetString ( offsetString )
The abstract operation IsTimeZoneOffsetString takes argument offsetString (a String) and returns a Boolean. 返回值指示 offsetString 是否符合 UTCOffset 给出的文法。 It performs the following steps when called:
令 parseResult 为 ParseText (offsetString , UTCOffset )。 如果 parseResult 是错误列表,则返回 false 。 返回 true 。
21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString )
The abstract operation ParseTimeZoneOffsetString takes argument offsetString (a String) and returns an integer . 返回值是与 String offsetString 对应的 UTC 偏移,表示为纳秒数。 It performs the following steps when called:
令 parseResult 为 ParseText (offsetString , UTCOffset )。 断言:parseResult 不是错误列表。 断言:parseResult 包含 一个 ASCIISign Parse Node 。 令 parsedSign 为 parseResult 中包含 的 ASCIISign Parse Node 所匹配的源文本 。 如果 parsedSign 是单个码点 U+002D(HYPHEN-MINUS),则令 sign 为 -1。 否则,令 sign 为 1。 注:下面 StringToNumber 的应用不会损失精度,因为每个被解析的值都保证是足够短的十进制数字字符串。 断言:parseResult 包含 一个 Hour Parse Node 。 令 parsedHours 为 parseResult 中包含 的 Hour Parse Node 所匹配的源文本 。 令 hours 为 ℝ (StringToNumber (CodePointsToString (parsedHours )))。 如果 parseResult 不包含 MinuteSecond Parse Node ,则令 minutes 为 0。 否则,令 parsedMinutes 为 parseResult 中包含 的第一个 MinuteSecond Parse Node 所匹配的源文本 。 令 minutes 为 ℝ (StringToNumber (CodePointsToString (parsedMinutes )))。 如果 parseResult 不包含 两个 MinuteSecond Parse Node ,则令 seconds 为 0。 否则,令 parsedSeconds 为 parseResult 中包含 的第二个 MinuteSecond Parse Node 所匹配的源文本 。 令 seconds 为 ℝ (StringToNumber (CodePointsToString (parsedSeconds )))。 如果 parseResult 不包含 TemporalDecimalFraction Parse Node ,则令 nanoseconds 为 0。 否则,令 parsedFraction 为 parseResult 中包含 的 TemporalDecimalFraction Parse Node 所匹配的源文本 。 令 fraction 为 CodePointsToString (parsedFraction ) 和 "000000000" 的字符串连接 。 令 nanosecondsString 为 fraction 从 1 到 10 的子字符串 。 令 nanoseconds 为 ℝ (StringToNumber (nanosecondsString ))。 返回 sign × (((hours × 60 + minutes ) × 60 + seconds ) × 109 + nanoseconds )。
21.4.4 Date 原型对象的属性
Date 原型对象 :
是 %Date.prototype% 。
自身是普通对象 。
不是 Date 实例,并且没有 [[DateValue]] 内部槽。
具有一个 [[Prototype]] 内部槽,其值为 %Object.prototype% 。
除非另有明确定义,下文定义的 Date 原型对象方法不是泛型的,并且传给它们的 this 值必须是一个对象,该对象具有已初始化为时间值 的 [[DateValue]] 内部槽。
21.4.4.1 Date.prototype.constructor
Date.prototype.constructor 的初始值是 %Date% 。
21.4.4.2 Date.prototype.getDate ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 DateFromTime (LocalTime (tv ))。
21.4.4.3 Date.prototype.getDay ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 WeekDay (LocalTime (tv ))。
21.4.4.4 Date.prototype.getFullYear ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 YearFromTime (LocalTime (tv ))。
21.4.4.5 Date.prototype.getHours ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 HourFromTime (LocalTime (tv ))。
21.4.4.6 Date.prototype.getMilliseconds ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 msFromTime (LocalTime (tv ))。
21.4.4.7 Date.prototype.getMinutes ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 MinFromTime (LocalTime (tv ))。
21.4.4.8 Date.prototype.getMonth ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 MonthFromTime (LocalTime (tv ))。
21.4.4.9 Date.prototype.getSeconds ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 SecFromTime (LocalTime (tv ))。
21.4.4.10 Date.prototype.getTime ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 返回 dateObj .[[DateValue]] 。
21.4.4.11 Date.prototype.getTimezoneOffset ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 (tv - LocalTime (tv )) / msPerMinute 。
21.4.4.12 Date.prototype.getUTCDate ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 DateFromTime (tv )。
21.4.4.13 Date.prototype.getUTCDay ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 WeekDay (tv )。
21.4.4.14 Date.prototype.getUTCFullYear ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 YearFromTime (tv )。
21.4.4.15 Date.prototype.getUTCHours ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 HourFromTime (tv )。
21.4.4.16 Date.prototype.getUTCMilliseconds ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 msFromTime (tv )。
21.4.4.17 Date.prototype.getUTCMinutes ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 MinFromTime (tv )。
21.4.4.18 Date.prototype.getUTCMonth ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 MonthFromTime (tv )。
21.4.4.19 Date.prototype.getUTCSeconds ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 NaN 。 返回 SecFromTime (tv )。
21.4.4.20 Date.prototype.setDate ( date )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 dt 为 ? ToNumber (date )。 如果 tv 是 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 令 newDate 为 MakeDate (MakeDay (YearFromTime (tv ), MonthFromTime (tv ), dt ), TimeWithinDay (tv ))。 令 u 为 TimeClip (UTC (newDate ))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
21.4.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 y 为 ? ToNumber (year )。 如果 tv 是 NaN ,则将 tv 设置为 +0 𝔽 ;否则将 tv 设置为 LocalTime (tv )。 如果 month 存在,则令 m 为 ? ToNumber (month );否则令 m 为 MonthFromTime (tv )。 如果 date 存在,则令 dt 为 ? ToNumber (date );否则令 dt 为 DateFromTime (tv )。 令 newDate 为 MakeDate (MakeDay (y , m , dt ), TimeWithinDay (tv ))。 令 u 为 TimeClip (UTC (newDate ))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
此方法的 "length" 属性是 3 𝔽 。
Note
如果 month 不存在,此方法表现得仿佛 month 存在且其值为 getMonth()。如果 date 不存在,它表现得仿佛 date 存在且其值为 getDate()。
21.4.4.22 Date.prototype.setHours ( hour [ , min [ , sec [ , ms ] ] ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 h 为 ? ToNumber (hour )。 如果 min 存在,则令 m 为 ? ToNumber (min )。 如果 sec 存在,则令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 如果 min 不存在,则令 m 为 MinFromTime (tv )。 如果 sec 不存在,则令 s 为 SecFromTime (tv )。 如果 ms 不存在,则令 milli 为 msFromTime (tv )。 令 date 为 MakeDate (Day (tv ), MakeTime (h , m , s , milli ))。 令 u 为 TimeClip (UTC (date ))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
此方法的 "length" 属性是 4 𝔽 。
Note
如果 min 不存在,此方法表现得仿佛 min 存在且其值为 getMinutes()。如果 sec 不存在,它表现得仿佛 sec 存在且其值为 getSeconds()。如果 ms 不存在,它表现得仿佛 ms 存在且其值为 getMilliseconds()。
21.4.4.23 Date.prototype.setMilliseconds ( ms )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 将 ms 设置为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 令 time 为 MakeTime (HourFromTime (tv ), MinFromTime (tv ), SecFromTime (tv ), ms )。 令 u 为 TimeClip (UTC (MakeDate (Day (tv ), time )))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
21.4.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 m 为 ? ToNumber (min )。 如果 sec 存在,则令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 如果 sec 不存在,则令 s 为 SecFromTime (tv )。 如果 ms 不存在,则令 milli 为 msFromTime (tv )。 令 date 为 MakeDate (Day (tv ), MakeTime (HourFromTime (tv ), m , s , milli ))。 令 u 为 TimeClip (UTC (date ))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
此方法的 "length" 属性是 3 𝔽 。
Note
如果 sec 不存在,此方法表现得仿佛 sec 存在且其值为 getSeconds()。如果 ms 不存在,它表现得仿佛 ms 存在且其值为 getMilliseconds()。
21.4.4.25 Date.prototype.setMonth ( month [ , date ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 m 为 ? ToNumber (month )。 如果 date 存在,则令 dt 为 ? ToNumber (date )。 如果 tv 是 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 如果 date 不存在,则令 dt 为 DateFromTime (tv )。 令 newDate 为 MakeDate (MakeDay (YearFromTime (tv ), m , dt ), TimeWithinDay (tv ))。 令 u 为 TimeClip (UTC (newDate ))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
此方法的 "length" 属性是 2 𝔽 。
Note
如果 date 不存在,此方法表现得仿佛 date 存在且其值为 getDate()。
21.4.4.26 Date.prototype.setSeconds ( sec [ , ms ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 如果 ms 不存在,则令 milli 为 msFromTime (tv )。 令 date 为 MakeDate (Day (tv ), MakeTime (HourFromTime (tv ), MinFromTime (tv ), s , milli ))。 令 u 为 TimeClip (UTC (date ))。 将 dateObj .[[DateValue]] 设置为 u 。 返回 u 。
此方法的 "length" 属性是 2 𝔽 。
Note
如果 ms 不存在,此方法表现得仿佛 ms 存在且其值为 getMilliseconds()。
21.4.4.27 Date.prototype.setTime ( time )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 t 为 ? ToNumber (time )。 令 v 为 TimeClip (t )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
21.4.4.28 Date.prototype.setUTCDate ( date )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 dt 为 ? ToNumber (date )。 如果 tv 是 NaN ,则返回 NaN 。 令 newDate 为 MakeDate (MakeDay (YearFromTime (tv ), MonthFromTime (tv ), dt ), TimeWithinDay (tv ))。 令 v 为 TimeClip (newDate )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
21.4.4.29 Date.prototype.setUTCFullYear ( year [ , month [ , date ] ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则将 tv 设置为 +0 𝔽 。 令 y 为 ? ToNumber (year )。 如果 month 存在,则令 m 为 ? ToNumber (month );否则令 m 为 MonthFromTime (tv )。 如果 date 存在,则令 dt 为 ? ToNumber (date );否则令 dt 为 DateFromTime (tv )。 令 newDate 为 MakeDate (MakeDay (y , m , dt ), TimeWithinDay (tv ))。 令 v 为 TimeClip (newDate )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
此方法的 "length" 属性是 3 𝔽 。
Note
如果 month 不存在,此方法表现得仿佛 month 存在且其值为 getUTCMonth()。如果 date 不存在,它表现得仿佛 date 存在且其值为 getUTCDate()。
21.4.4.30 Date.prototype.setUTCHours ( hour [ , min [ , sec [ , ms ] ] ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 h 为 ? ToNumber (hour )。 如果 min 存在,则令 m 为 ? ToNumber (min )。 如果 sec 存在,则令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 如果 min 不存在,则令 m 为 MinFromTime (tv )。 如果 sec 不存在,则令 s 为 SecFromTime (tv )。 如果 ms 不存在,则令 milli 为 msFromTime (tv )。 令 date 为 MakeDate (Day (tv ), MakeTime (h , m , s , milli ))。 令 v 为 TimeClip (date )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
此方法的 "length" 属性是 4 𝔽 。
Note
如果 min 不存在,此方法表现得仿佛 min 存在且其值为 getUTCMinutes()。如果 sec 不存在,它表现得仿佛 sec 存在且其值为 getUTCSeconds()。如果 ms 不存在,它表现得仿佛 ms 存在且其值为 getUTCMilliseconds()。
21.4.4.31 Date.prototype.setUTCMilliseconds ( ms )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 将 ms 设置为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 令 time 为 MakeTime (HourFromTime (tv ), MinFromTime (tv ), SecFromTime (tv ), ms )。 令 v 为 TimeClip (MakeDate (Day (tv ), time ))。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
21.4.4.32 Date.prototype.setUTCMinutes ( min [ , sec [ , ms ] ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 m 为 ? ToNumber (min )。 如果 sec 存在,则令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 如果 sec 不存在,则令 s 为 SecFromTime (tv )。 如果 ms 不存在,则令 milli 为 msFromTime (tv )。 令 date 为 MakeDate (Day (tv ), MakeTime (HourFromTime (tv ), m , s , milli ))。 令 v 为 TimeClip (date )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
此方法的 "length" 属性是 3 𝔽 。
Note
如果 sec 不存在,此方法表现得仿佛 sec 存在且其值为 getUTCSeconds()。如果 ms 不存在,它表现得仿佛 ms 存在且其值为 getUTCMilliseconds() 返回的值。
21.4.4.33 Date.prototype.setUTCMonth ( month [ , date ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 m 为 ? ToNumber (month )。 如果 date 存在,则令 dt 为 ? ToNumber (date )。 如果 tv 是 NaN ,则返回 NaN 。 如果 date 不存在,则令 dt 为 DateFromTime (tv )。 令 newDate 为 MakeDate (MakeDay (YearFromTime (tv ), m , dt ), TimeWithinDay (tv ))。 令 v 为 TimeClip (newDate )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
此方法的 "length" 属性是 2 𝔽 。
Note
如果 date 不存在,此方法表现得仿佛 date 存在且其值为 getUTCDate()。
21.4.4.34 Date.prototype.setUTCSeconds ( sec [ , ms ] )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 是 NaN ,则返回 NaN 。 如果 ms 不存在,则令 milli 为 msFromTime (tv )。 令 date 为 MakeDate (Day (tv ), MakeTime (HourFromTime (tv ), MinFromTime (tv ), s , milli ))。 令 v 为 TimeClip (date )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
此方法的 "length" 属性是 2 𝔽 。
Note
如果 ms 不存在,此方法表现得仿佛 ms 存在且其值为 getUTCMilliseconds()。
21.4.4.35 Date.prototype.toDateString ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 "Invalid Date" 。 令 t 为 LocalTime (tv )。 返回 DateString (t )。
21.4.4.36 Date.prototype.toISOString ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则抛出 RangeError 异常。 断言:tv 是整数 Number。 如果 tv 对应于不能在日期时间字符串格式 中表示的年份,则抛出 RangeError 异常。 返回 tv 在 UTC 时间尺度上的日期时间字符串格式 中的 String 表示,包括所有格式元素和 UTC 偏移表示 "Z" 。
21.4.4.37 Date.prototype.toJSON ( key )
此方法提供 Date 的 String 表示,以供 JSON.stringify(25.5.4 )使用。
它在被调用时执行以下步骤:
令 obj 为 ? ToObject (this value)。 令 tv 为 ? ToPrimitive (obj , number )。 如果 tv 是 Number 且 tv 不是有限 的,则返回 null 。 返回 ? Invoke (obj , "toISOString" )。
Note 1
Note 2
此方法有意设计为泛型的;它不要求其 this 值是 Date。因此,它可以被转移到其他种类的对象上作为方法使用。不过,它要求任何这样的对象都具有 toISOString 方法。
21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用此方法的以下规范:
此方法返回一个 String 值。该 String 的内容由实现定义,但旨在以方便且人类可读的形式表示当前时区中 Date 的“日期”部分,并对应于宿主环境 当前区域设置的约定。
此方法可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得把这些参数位置用于任何其他用途。
21.4.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用此方法的以下规范:
此方法返回一个 String 值。该 String 的内容由实现定义,但旨在以方便且人类可读的形式表示当前时区中的 Date,并对应于宿主环境 当前区域设置的约定。
此方法可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得把这些参数位置用于任何其他用途。
21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用此方法的以下规范:
此方法返回一个 String 值。该 String 的内容由实现定义,但旨在以方便且人类可读的形式表示当前时区中 Date 的“时间”部分,并对应于宿主环境 当前区域设置的约定。
此方法可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得把这些参数位置用于任何其他用途。
21.4.4.41 Date.prototype.toString ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 返回 ToDateString (tv )。
Note 1
对于任何 Date d,如果 d.[[DateValue]] 可被 1000 整除,则 Date.parse(d.toString()) 的结果 = d.valueOf()。见 21.4.3.2 。
Note 2
此方法不是泛型的;如果其 this 值不是 Date,它会抛出 TypeError 异常。因此,它不能被转移到其他种类的对象上作为方法使用。
21.4.4.41.1 TimeString ( tv )
The abstract operation TimeString takes argument tv (a Number, but not NaN ) and returns a String. It performs the following steps when called:
令 hour 为 ToZeroPaddedDecimalString (ℝ (HourFromTime (tv )), 2)。 令 minute 为 ToZeroPaddedDecimalString (ℝ (MinFromTime (tv )), 2)。 令 second 为 ToZeroPaddedDecimalString (ℝ (SecFromTime (tv )), 2)。 返回 hour 、":" 、minute 、":" 、second 、码元 0x0020(SPACE)和 "GMT" 的字符串连接 。
21.4.4.41.2 DateString ( tv )
The abstract operation DateString takes argument tv (a Number, but not NaN ) and returns a String. It performs the following steps when called:
令 weekday 为 Table 61 中 Number 为 WeekDay (tv ) 的条目的 Name。 令 month 为 Table 62 中 Number 为 MonthFromTime (tv ) 的条目的 Name。 令 day 为 ToZeroPaddedDecimalString (ℝ (DateFromTime (tv )), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv 是 +0 𝔽 或 yv > +0 𝔽 ,则令 yearSign 为空 String;否则令 yearSign 为 "-" 。 令 paddedYear 为 ToZeroPaddedDecimalString (abs (ℝ (yv )), 4)。 返回 weekday 、码元 0x0020(SPACE)、month 、码元 0x0020(SPACE)、day 、码元 0x0020(SPACE)、yearSign 和 paddedYear 的字符串连接 。
Table 61: 星期中各天的名称
Number
Name
+0 𝔽
"Sun"
1 𝔽
"Mon"
2 𝔽
"Tue"
3 𝔽
"Wed"
4 𝔽
"Thu"
5 𝔽
"Fri"
6 𝔽
"Sat"
Table 62: 一年中各月份的名称
Number
Name
+0 𝔽
"Jan"
1 𝔽
"Feb"
2 𝔽
"Mar"
3 𝔽
"Apr"
4 𝔽
"May"
5 𝔽
"Jun"
6 𝔽
"Jul"
7 𝔽
"Aug"
8 𝔽
"Sep"
9 𝔽
"Oct"
10 𝔽
"Nov"
11 𝔽
"Dec"
21.4.4.41.3 TimeZoneString ( tv )
The abstract operation TimeZoneString takes argument tv (an integral Number ) and returns a String. It performs the following steps when called:
令 systemTimeZoneIdentifier 为 SystemTimeZoneIdentifier ()。 如果 IsTimeZoneOffsetString (systemTimeZoneIdentifier ) 是 true ,则令 offsetNs 为 ParseTimeZoneOffsetString (systemTimeZoneIdentifier )。 否则,令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , ℤ (ℝ (tv ) × 106 ))。 令 offset 为 𝔽 (truncate (offsetNs / 106 ))。 如果 offset 是 +0 𝔽 或 offset > +0 𝔽 ,则令 offsetSign 为 "+" 。 令 absOffset 为 offset 。 否则,令 offsetSign 为 "-" 。 令 absOffset 为 -offset 。 令 offsetMin 为 ToZeroPaddedDecimalString (ℝ (MinFromTime (absOffset )), 2)。 令 offsetHour 为 ToZeroPaddedDecimalString (ℝ (HourFromTime (absOffset )), 2)。 令 tzName 为一个由实现定义的 字符串,它要么是空 String,要么是码元 0x0020(SPACE)、码元 0x0028(LEFT PARENTHESIS)、一个由实现定义的 时区名称和码元 0x0029(RIGHT PARENTHESIS)的字符串连接 。 返回 offsetSign 、offsetHour 、offsetMin 和 tzName 的字符串连接 。
21.4.4.41.4 ToDateString ( tv )
The abstract operation ToDateString takes argument tv (an integral Number or NaN ) and returns a String. It performs the following steps when called:
如果 tv 是 NaN ,则返回 "Invalid Date" 。 令 localTime 为 LocalTime (tv )。 返回 DateString (localTime )、码元 0x0020(SPACE)、TimeString (localTime ) 和 TimeZoneString (tv ) 的字符串连接 。
21.4.4.42 Date.prototype.toTimeString ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 "Invalid Date" 。 令 localTime 为 LocalTime (tv )。 返回 TimeString (localTime ) 和 TimeZoneString (tv ) 的字符串连接 。
21.4.4.43 Date.prototype.toUTCString ( )
此方法返回一个 String 值,表示与 this 值对应的时间瞬间。该 String 的格式基于 RFC 7231 中的 HTTP-date,并泛化为支持 ECMAScript Date 所支持的完整时间范围。
它在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 是 NaN ,则返回 "Invalid Date" 。 令 weekday 为 Table 61 中 Number 为 WeekDay (tv ) 的条目的 Name。 令 month 为 Table 62 中 Number 为 MonthFromTime (tv ) 的条目的 Name。 令 day 为 ToZeroPaddedDecimalString (ℝ (DateFromTime (tv )), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv 是 +0 𝔽 或 yv > +0 𝔽 ,则令 yearSign 为空 String;否则令 yearSign 为 "-" 。 令 paddedYear 为 ToZeroPaddedDecimalString (abs (ℝ (yv )), 4)。 返回 weekday 、"," 、码元 0x0020(SPACE)、day 、码元 0x0020(SPACE)、month 、码元 0x0020(SPACE)、yearSign 、paddedYear 、码元 0x0020(SPACE)和 TimeString (tv ) 的字符串连接 。
21.4.4.44 Date.prototype.valueOf ( )
此方法在被调用时执行以下步骤:
令 dateObj 为 this value。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 返回 dateObj .[[DateValue]] 。
21.4.4.45 Date.prototype [ %Symbol.toPrimitive% ] ( hint )
此方法由 ECMAScript 语言运算符调用,用于将 Date 转换为原始值。hint 的允许值是 "default" 、"number" 和 "string" 。Date 在内置 ECMAScript 对象中是独特的,因为它们把 "default" 视为等同于 "string" ;所有其他内置 ECMAScript 对象都把 "default" 视为等同于 "number" 。
它在被调用时执行以下步骤:
令 obj 为 this value。 如果 obj 不是 Object,则抛出 TypeError 异常。 如果 hint 是 "string" 或 "default" ,则令 tryFirst 为 string 。 否则,如果 hint 是 "number" ,则令 tryFirst 为 number 。 否则,抛出 TypeError 异常。 返回 ? OrdinaryToPrimitive (obj , tryFirst )。
此属性具有特性 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true }。
此方法的 "name" 属性的值是 "[Symbol.toPrimitive]" 。