21.4.1 Date 对象概述及抽象操作的定义
以下抽象操作 对时间值 (定义见 21.4.1.1 )进行操作。请注意,在所有情况下,如果这些函数中任一函数的任何参数是 NaN ,则结果将为 NaN 。
21.4.1.1 时间值和时间范围
ECMAScript 中的时间度量类似于 POSIX 中的时间度量,特别是都依据公历回溯历法进行定义,以 UTC 1970 年 1 月 1 日开始时的午夜作为纪元 ,并将每一天计为恰好包含 86,400 秒(每秒为 1000 毫秒)。
ECMAScript 时间值 是一个 Number,它可以是表示精确到毫秒的某个时刻的有限 整数 Number,也可以是表示没有特定时刻的 NaN 。作为 msPerDay 整数倍的时间值(即对于某个整数 d ,其值为 msPerDay × d )表示比纪元 晚 d 个完整 UTC 日的 UTC 日开始时的时刻(当 d 为负数时则早于纪元 )。其他任何有限 时间值 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
NanosecondsPerDay = 10
6 ×
msPerDay = 8.64 × 10
13
nsPerMillisecond = 106
nsPerMicrosecond = 103
21.4.1.3 Day ( tv )
The abstract operation Day takes argument tv (一个有限 时间值 ) and returns 一个整数. 它返回 tv 所在日期的日编号。 It performs the following steps when called:
返回 floor (ℝ (tv ) / msPerDay )。
21.4.1.4 TimeWithinDay ( tv )
The abstract operation TimeWithinDay takes argument tv (一个有限 时间值 ) and returns 一个处于从 0(含)到 msPerDay (不含)区间 内的整数. 它返回自 tv 所在日期开始以来的毫秒数。 It performs the following steps when called:
返回 ℝ (tv ) 模 msPerDay 。
21.4.1.5 DayFromYear ( y )
The abstract operation DayFromYear takes argument y (一个整数) and returns 一个整数. 它返回年份 y 第一天的日编号。 It performs the following steps when called:
注:在以下步骤中,numberYears1 、numberYears4 、numberYears100 和 numberYears400 分别表示在纪元 与年份 y 开始之间出现的可被 1、4、100 和 400 整除的年份数量。如果 y 位于纪元 之前,则该数量为负数。 令 numberYears1 为 (y - 1970)。 令 numberYears4 为 floor ((y - 1969) / 4)。 令 numberYears100 为 floor ((y - 1901) / 100)。 令 numberYears400 为 floor ((y - 1601) / 400)。 返回 365 × numberYears1 + numberYears4 - numberYears100 + numberYears400 。
21.4.1.6 TimeFromYear ( y )
The abstract operation TimeFromYear takes argument y (一个整数) and returns 一个时间值 . 它返回年份 y 开始时的时间值 。 It performs the following steps when called:
返回 𝔽 (msPerDay × DayFromYear (y ))。
21.4.1.7 YearFromTime ( tv )
The abstract operation YearFromTime takes argument tv (一个有限 时间值 ) and returns 一个整数. 它返回 tv 所在的年份。 It performs the following steps when called:
返回满足 TimeFromYear (y ) ≤ tv 的最大整数 y (最接近 +∞)。
21.4.1.8 DayWithinYear ( tv )
The abstract operation DayWithinYear takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 365 的闭区间 内的整数. It performs the following steps when called:
返回 Day (tv ) - DayFromYear (YearFromTime (tv ))。
21.4.1.9 InLeapYear ( tv )
The abstract operation InLeapYear takes argument tv (一个有限 时间值 ) and returns 0 或 1. 如果 tv 位于闰年中,则返回 1,否则返回 0。 It performs the following steps when called:
令 y 为 YearFromTime (tv )。 如果(y 模 400)= 0,则返回 1。 如果(y 模 100)= 0,则返回 0。 如果(y 模 4)= 0,则返回 1。 返回 0。
21.4.1.10 MonthFromTime ( tv )
The abstract operation MonthFromTime takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 11 的闭区间 内的整数. 它返回一个标识 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.11 DateFromTime ( tv )
The abstract operation DateFromTime takes argument tv (一个有限 时间值 ) and returns 一个处于从 1 到 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.12 WeekDay ( tv )
The abstract operation WeekDay takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 6 的闭区间 内的整数. 它返回一个标识 tv 所在星期几的整数。星期值 0 表示星期日;1 表示星期一;2 表示星期二;3 表示星期三;4 表示星期四;5 表示星期五;6 表示星期六。请注意,WeekDay(+0 𝔽 ) = 4 ,对应于 1970 年 1 月 1 日星期四。 It performs the following steps when called:
返回 (Day (tv ) + 4) 模 7。
21.4.1.13 HourFromTime ( tv )
The abstract operation HourFromTime takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 23 的闭区间 内的整数. 它返回 tv 所在日期中的小时。 It performs the following steps when called:
返回 floor (ℝ (tv ) / msPerHour ) 模 HoursPerDay 。
21.4.1.14 MinFromTime ( tv )
The abstract operation MinFromTime takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 59 的闭区间 内的整数. 它返回 tv 所在小时中的分钟。 It performs the following steps when called:
返回 floor (ℝ (tv ) / msPerMinute ) 模 MinutesPerHour 。
21.4.1.15 SecFromTime ( tv )
The abstract operation SecFromTime takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 59 的闭区间 内的整数. 它返回 tv 所在分钟中的秒。 It performs the following steps when called:
返回 floor (ℝ (tv ) / msPerSecond ) 模 SecondsPerMinute 。
21.4.1.16 MillisecFromTime ( tv )
The abstract operation MillisecFromTime takes argument tv (一个有限 时间值 ) and returns 一个处于从 0 到 999 的闭区间 内的整数. 它返回 tv 所在秒中的毫秒。 It performs the following steps when called:
返回 ℝ (tv ) 模 msPerSecond 。
21.4.1.17 纪元纳秒与范围
纪元纳秒计数 是一个以纳秒精度表示时间瞬时的整数。它支持与时间值 相同的范围,但以纳秒表示,即从 MinEpochNanoseconds 到 MaxEpochNanoseconds 。时间值 NaN 没有对应的纳秒等价值;不存在表示非特定瞬时的形式。
UTC 1970 年 1 月 1 日开始时的午夜确切时刻由值 0 表示。最大值为 MaxEpochNanoseconds ,最小值为 MinEpochNanoseconds 。
21.4.1.18 GetUTCEpochNanoseconds ( year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )
The abstract operation GetUTCEpochNanoseconds takes arguments year (一个整数), month (1 到 12 的闭区间 内的整数), day (1 到 31 的闭区间 内的整数), hour (0 到 23 的闭区间 内的整数), minute (0 到 59 的闭区间 内的整数), second (0 到 59 的闭区间 内的整数), millisecond (0 到 999 的闭区间 内的整数), microsecond (0 到 999 的闭区间 内的整数), and nanosecond (0 到 999 的闭区间 内的整数) and returns 一个纪元纳秒计数 . 返回值是与给定的 UTC ISO 8601 日历日期和钟表时间相对应的纪元纳秒计数 。 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 ) × nsPerMillisecond + microsecond × nsPerMicrosecond + nanosecond 。
21.4.1.19 时区标识符
ECMAScript 中的时区由时区标识符 表示,该标识符是完全由位于从 0x0000 到 0x007F 的闭区间 内的代码单元组成的 String。
ECMAScript 实现所支持的时区可以是可用具名时区 ,由 AvailableNamedTimeZoneIdentifiers 返回的 Time Zone Identifier Record 的 [[Identifier]] 字段表示;也可以是偏移时区 ,由使 IsTimeZoneOffsetString 返回 true 的 String 表示。
主时区标识符 是可用具名时区的首选标识符。
非主时区标识符 是可用具名时区的标识符,但不是主时区标识符。
可用具名时区标识符 是主时区标识符或非主时区标识符。
每个可用具名时区标识符恰好与一个可用具名时区相关联。
每个可用具名时区恰好与一个主时区标识符以及零个或多个非主时区标识符相关联。
ECMAScript 实现必须支持标识符为 "UTC" 的可用具名时区,该标识符必须是 UTC 时区的主时区标识符。
此外,实现可以支持任意数量的其他可用具名时区。
遵循 ECMA-402 国际化 API 规范中所述时区要求的实现称为时区感知 实现。
时区感知实现必须支持与 IANA 时区数据库 中的“Zone”和“Link ”名称相对应的可用命名时区,并且只能支持这些名称。
在时区感知实现中,主时区标识符是 IANA 时区数据库中的“Zone”名称,而非主时区标识符则是“Link ”名称,除非 ECMA-402 规范中指定的 AvailableNamedTimeZoneIdentifiers 明确另有规定。
对于不支持整个 IANA 时区数据库的实现,仍建议使用 IANA 时区数据库名称作为表示时区的标识符。
21.4.1.20 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier , year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )
The implementation-defined abstract operation GetNamedTimeZoneEpochNanoseconds takes arguments timeZoneIdentifier (一个 String), year (一个整数), month (1 到 12 的闭区间 内的整数), day (1 到 31 的闭区间 内的整数), hour (0 到 23 的闭区间 内的整数), minute (0 到 59 的闭区间 内的整数), second (0 到 59 的闭区间 内的整数), millisecond (0 到 999 的闭区间 内的整数), microsecond (0 到 999 的闭区间 内的整数), and nanosecond (0 到 999 的闭区间 内的整数) and returns 纪元纳秒计数 的 List.
返回的 List 中的每个值都表示一个与给定的 ISO 8601 日历日期以及由 timeZoneIdentifier 标识的命名时区中的钟表时间相对应的纪元纳秒计数 。
当输入表示因负向时区转换而多次出现的本地时间时(例如夏令时结束,或因时区规则更改而减小时区偏移量),返回的 List 将包含 多个元素,并按数值升序排序。
当输入表示因正向时区转换而被跳过的本地时间时(例如夏令时开始,或因时区规则更改而增大时区偏移量),返回的 List 将为空。
否则,返回的 List 将包含 一个元素。
GetNamedTimeZoneEpochNanoseconds 的默认实现供不包含 任何时区的当地政治规则的 ECMAScript 实现使用,其在被调用时执行以下步骤:
断言:timeZoneIdentifier 为 "UTC" 。 令 epochNanoseconds 为 GetUTCEpochNanoseconds (year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )。 返回 « epochNanoseconds »。
Note
时区感知 实现必须使用 IANA 时区数据库 的时区信息(并建议所有其他实现也这样做)。
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 (一个 String) and epochNanoseconds (一个纪元纳秒计数 ) and returns 一个整数.
返回的整数表示在与纪元纳秒计数 epochNanoseconds 相对应的瞬时,由 timeZoneIdentifier 标识的命名时区相对于 UTC 的偏移量,以纳秒为单位。
GetNamedTimeZoneOffsetNanoseconds 的默认实现供不包含 任何时区的当地政治规则的 ECMAScript 实现使用,其在被调用时执行以下步骤:
断言:timeZoneIdentifier 为 "UTC" 。 返回 0。
Note
21.4.1.22 时区标识符记录
时区标识符记录 是一种用于描述可用具名时区标识符 及其对应主时区标识符 的 Record。
时区标识符记录具有 Table 60 中列出的字段。
Table 60: 时区标识符记录 的字段
字段名称
值
含义
[[Identifier]]
一个 String
实现所支持的可用具名时区标识符 。
[[PrimaryIdentifier]]
一个 String
[[Identifier]] 解析到的主时区标识符 。
Note
如果 [[Identifier]] 是主时区标识符 ,则 [[Identifier]] 就是 [[PrimaryIdentifier]] 。
21.4.1.23 AvailableNamedTimeZoneIdentifiers ( )
The implementation-defined abstract operation AvailableNamedTimeZoneIdentifiers takes no arguments and returns Time Zone Identifier Record 的 List.
其结果描述此实现中的所有可用具名时区标识符 ,以及与每个可用具名时区标识符 相对应的主时区标识符 。
该 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 一个 String.
它返回一个表示宿主环境 当前时区的 String,该 String 要么表示一个使 IsTimeZoneOffsetString 返回 true 的 UTC 偏移,要么是一个主时区标识符 。
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 (一个有限 时间值 ) and returns 一个整数 Number.
它将 tv 从 UTC 转换为本地时间。
应使用在 tv 时生效的标准时间和夏令时本地政治规则,按照本节规定的方式确定结果。
It performs the following steps when called:
令 systemTimeZoneIdentifier 为 SystemTimeZoneIdentifier ()。 如果 IsTimeZoneOffsetString (systemTimeZoneIdentifier ) 为 true ,则令 offsetNs 为 ParseTimeZoneOffsetString (systemTimeZoneIdentifier )。 否则,令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , ℝ (tv ) × nsPerMillisecond )。 令 offsetMs 为 truncate (offsetNs / nsPerMillisecond )。 返回 tv + 𝔽 (offsetMs )。
Note 1
Note 2
时区感知 实现必须使用 IANA 时区数据库 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 (一个 Number) and returns 一个时间值 .
它将 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 ), MillisecFromTime (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 ), MillisecFromTime (tBefore ), 0, 0),其中 tBefore 是使 possibleInstantsBefore 非空的最大整数 Number < t (即,tBefore 表示转换前的最后一个本地时间)。 令 disambiguatedInstant 为 possibleInstantsBefore 的最后一个元素。 令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , disambiguatedInstant )。 令 offsetMs 为 truncate (offsetNs / nsPerMillisecond )。 返回 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 时区数据库 的时区信息(并建议所有其他实现也这样做)。
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 (一个 Number), min (一个 Number), sec (一个 Number), and ms (一个 Number) and returns 一个 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 (一个 Number), month (一个 Number), and date (一个 Number) and returns 一个有限 Number 或 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 模 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 (一个 Number) and time (一个 Number) and returns 一个有限 Number 或 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 (一个 Number) and returns 一个整数 Number 或 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 (一个 Number) and returns 一个时间值 . 它计算毫秒数。 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。
Note 2
此格式不支持 RFC 9557 中定义的带有时区名称的注解,仅支持时区偏移的数字表示。
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
包含 扩展年份的日期时间值 示例:
-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 (一个 String) and returns 一个 Boolean. 返回值表示 offsetString 是否符合 UTCOffset 给出的语法。 It performs the following steps when called:
令 parseResult 为 ParseText (offsetString , UTCOffset )。 如果 parseResult 是一个错误 List,则返回 false 。 返回 true 。
21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString )
The abstract operation ParseTimeZoneOffsetString takes argument offsetString (一个 String) and returns 一个整数. 返回值是与 String offsetString 相对应的 UTC 偏移,以纳秒数表示。 It performs the following steps when called:
令 parseResult 为 ParseText (offsetString , UTCOffset )。 断言:parseResult 不是一个错误 List。 断言: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 × MinutesPerHour + minutes ) × SecondsPerMinute + seconds ) × nsPerSecond + 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 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (DateFromTime (LocalTime (tv )))。
21.4.4.3 Date.prototype.getDay ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (WeekDay (LocalTime (tv )))。
21.4.4.4 Date.prototype.getFullYear ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (YearFromTime (LocalTime (tv )))。
21.4.4.5 Date.prototype.getHours ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (HourFromTime (LocalTime (tv )))。
21.4.4.6 Date.prototype.getMilliseconds ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (MillisecFromTime (LocalTime (tv )))。
21.4.4.7 Date.prototype.getMinutes ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (MinFromTime (LocalTime (tv )))。
21.4.4.8 Date.prototype.getMonth ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (MonthFromTime (LocalTime (tv )))。
21.4.4.9 Date.prototype.getSeconds ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (SecFromTime (LocalTime (tv )))。
21.4.4.10 Date.prototype.getTime ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 返回 dateObj .[[DateValue]] 。
21.4.4.11 Date.prototype.getTimezoneOffset ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 (tv - LocalTime (tv )) / 𝔽 (msPerMinute )。
21.4.4.12 Date.prototype.getUTCDate ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (DateFromTime (tv ))。
21.4.4.13 Date.prototype.getUTCDay ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (WeekDay (tv ))。
21.4.4.14 Date.prototype.getUTCFullYear ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (YearFromTime (tv ))。
21.4.4.15 Date.prototype.getUTCHours ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (HourFromTime (tv ))。
21.4.4.16 Date.prototype.getUTCMilliseconds ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (MillisecFromTime (tv ))。
21.4.4.17 Date.prototype.getUTCMinutes ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (MinFromTime (tv ))。
21.4.4.18 Date.prototype.getUTCMonth ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (MonthFromTime (tv ))。
21.4.4.19 Date.prototype.getUTCSeconds ( )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 NaN 。 返回 𝔽 (SecFromTime (tv ))。
21.4.4.20 Date.prototype.setDate ( date )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? 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 值。 执行 ? 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 值。 执行 ? 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 为 𝔽 (MillisecFromTime (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 值。 执行 ? 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 值。 执行 ? 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 为 𝔽 (MillisecFromTime (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 值。 执行 ? 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 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 为 NaN ,则返回 NaN 。 将 tv 设置为 LocalTime (tv )。 如果 ms 不存在,则令 milli 为 𝔽 (MillisecFromTime (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 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 t 为 ? ToNumber (time )。 令 v 为 TimeClip (t )。 将 dateObj .[[DateValue]] 设置为 v 。 返回 v 。
21.4.4.28 Date.prototype.setUTCDate ( date )
调用此方法时,它执行以下步骤:
令 dateObj 为 this 值。 执行 ? 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 值。 执行 ? 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 值。 执行 ? 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 为 𝔽 (MillisecFromTime (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 值。 执行 ? 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 值。 执行 ? 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 为 𝔽 (MillisecFromTime (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 值。 执行 ? 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 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 令 s 为 ? ToNumber (sec )。 如果 ms 存在,则令 milli 为 ? ToNumber (ms )。 如果 tv 为 NaN ,则返回 NaN 。 如果 ms 不存在,则令 milli 为 𝔽 (MillisecFromTime (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 (一个 Number,但不是 NaN ) and returns 一个 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 (一个 Number,但不是 NaN ) and returns 一个 String. It performs the following steps when called:
令 weekday 为 Table 61 中 WeekDay Index = WeekDay (tv ) 的条目的 Name。 令 month 为 Table 62 中 Month Index = MonthFromTime (tv ) 的条目的 Name。 令 day 为 ToZeroPaddedDecimalString (DateFromTime (tv ), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv ≥ 0,则令 yearSign 为空 String;否则令 yearSign 为 "-" 。 令 paddedYear 为 ToZeroPaddedDecimalString (abs (yv ), 4)。 返回 weekday 、代码单元 0x0020 (SPACE)、month 、代码单元 0x0020 (SPACE)、day 、代码单元 0x0020 (SPACE)、yearSign 和 paddedYear 的字符串拼接结果。
Table 61: 星期名称
WeekDay 索引
名称
0
"Sun"
1
"Mon"
2
"Tue"
3
"Wed"
4
"Thu"
5
"Fri"
6
"Sat"
Table 62: 一年中各月份的名称
月份索引
名称
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 (一个整数 Number) and returns 一个 String. It performs the following steps when called:
令 systemTimeZoneIdentifier 为 SystemTimeZoneIdentifier ()。 如果 IsTimeZoneOffsetString (systemTimeZoneIdentifier ) 为 true ,则令 offsetNs 为 ParseTimeZoneOffsetString (systemTimeZoneIdentifier )。 否则,令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , ℝ (tv ) × nsPerMillisecond )。 令 offset 为 truncate (offsetNs / nsPerMillisecond )。 如果 offset ≥ 0,则令 offsetSign 为 "+" 。 令 absOffset 为 offset 。 否则,令 offsetSign 为 "-" 。 令 absOffset 为 -offset 。 令 offsetMin 为 ToZeroPaddedDecimalString (MinFromTime (𝔽 (absOffset )), 2)。 令 offsetHour 为 ToZeroPaddedDecimalString (HourFromTime (𝔽 (absOffset )), 2)。 令 tzName 为一个实现定义的 字符串,该字符串要么是空 String,要么是代码单元 0x0020(空格)、代码单元 0x0028(左圆括号)、一个实现定义的 时区名称和代码单元 0x0029(右圆括号)的字符串连接 。 返回 offsetSign 、offsetHour 、offsetMin 和 tzName 的字符串连接 。
21.4.4.41.4 ToDateString ( tv )
The abstract operation ToDateString takes argument tv (一个整数 Number 或 NaN ) and returns 一个 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 值。 执行 ? RequireInternalSlot (dateObj , [[DateValue]] )。 令 tv 为 dateObj .[[DateValue]] 。 如果 tv 为 NaN ,则返回 "Invalid Date" 。 令 weekday 为 Table 61 中 WeekDay Index = WeekDay (tv ) 的条目的 Name。 令 month 为 Table 62 中 Month Index = MonthFromTime (tv ) 的条目的 Name。 令 day 为 ToZeroPaddedDecimalString (DateFromTime (tv ), 2)。 令 yv 为 YearFromTime (tv )。 如果 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]" 。