21.4.1 Date 对象概述及抽象操作定义
以下抽象操作 作用于时间值 (定义见 21.4.1.1 )。注意,在所有情况下,如果这些函数的任何参数为 NaN ,则结果为 NaN 。
21.4.1.1 时间值与时间范围
ECMAScript 的时间度量与 POSIX 的时间度量类似,特别是在使用前推的公历、以 1970 年 1 月 1 日 UTC 零点为 纪元 ,以及每一天精确包含 86,400 秒(每秒为 1000 毫秒)的定义上。
ECMAScript 的 时间值 是一个 Number,要么是表示某一时刻的有限 整数 Number(精确到毫秒),要么是 NaN 表示没有具体时刻。一个是 24 × 60 × 60 × 1000 = 86,400,000 的倍数(即对于某个整数 d ,为 86,400,000 × d )的时间值,表示自纪元 起经过 d 个完整 UTC 日的 UTC 日起始时刻(负 d 表示纪元 之前)。其他有限 时间值 t 则以紧邻其之前最大倍数的时间值 s 为基准,表示与 s 同一天内,距离 s 之后 (t - 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 毫秒。这对应于自 1970 年 1 月 1 日 UTC 零点起精确的 -100,000,000 天到 100,000,000 天的时间值范围。
1970 年 1 月 1 日 UTC 零点的精确时刻由时间值 +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 ( t )
The abstract operation Day takes argument t (有限 时间值 ) and returns 整数 Number. It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerDay )))。
21.4.1.4 TimeWithinDay ( t )
The abstract operation TimeWithinDay takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 (含)至 msPerDay (不含)内的整数 Number. It performs the following steps when called:
返回 𝔽 (ℝ (t ) 模 ℝ (msPerDay ))。
21.4.1.5 DaysInYear ( y )
The abstract operation DaysInYear takes argument y (整数 Number) and returns 365 𝔽 或 366 𝔽 . It performs the following steps when called:
令 ry 为 ℝ (y )。 若 (ry 模 400) = 0,返回 366 𝔽 。 若 (ry 模 100) = 0,返回 365 𝔽 。 若 (ry 模 4) = 0,返回 366 𝔽 。 返回 365 𝔽 。
21.4.1.6 DayFromYear ( y )
The abstract operation DayFromYear takes argument y (整数 Number) and returns 整数 Number. It performs the following steps when called:
令 ry 为 ℝ (y )。 注:下列步骤中,numYears1 、numYears4 、numYears100 、numYears400 表示自纪元 至 y 年首之间能被 1、4、100、400 整除的年份数。若 y 在纪元 之前则为负数。 令 numYears1 为 (ry - 1970)。 令 numYears4 为 floor ((ry - 1969) / 4)。 令 numYears100 为 floor ((ry - 1901) / 100)。 令 numYears400 为 floor ((ry - 1601) / 400)。 返回 𝔽 (365 × numYears1 + numYears4 - numYears100 + numYears400 )。
21.4.1.7 TimeFromYear ( y )
The abstract operation TimeFromYear takes argument y (整数 Number) and returns 时间值 . It performs the following steps when called:
返回 msPerDay × DayFromYear (y )。
21.4.1.8 YearFromTime ( t )
The abstract operation YearFromTime takes argument t (有限 时间值 ) and returns 整数 Number. It performs the following steps when called:
返回满足 TimeFromYear (y ) ≤ t 的最大整数 Number y (最接近 +∞)。
21.4.1.9 DayWithinYear ( t )
The abstract operation DayWithinYear takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 365 𝔽 (含)的整数 Number. It performs the following steps when called:
返回 Day (t ) - DayFromYear (YearFromTime (t ))。
21.4.1.10 InLeapYear ( t )
The abstract operation InLeapYear takes argument t (有限 时间值 ) and returns +0 𝔽 或 1 𝔽 . It performs the following steps when called:
若 DaysInYear (YearFromTime (t )) 为 366 𝔽 ,则返回 1 𝔽 ;否则返回 +0 𝔽 。
21.4.1.11 MonthFromTime ( t )
The abstract operation MonthFromTime takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 11 𝔽 (含)的整数 Number. It performs the following steps when called:
令 inLeapYear 为 InLeapYear (t )。 令 dayWithinYear 为 DayWithinYear (t )。 若 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 ( t )
The abstract operation DateFromTime takes argument t (有限 时间值 ) and returns 区间 1 𝔽 至 31 𝔽 (含)的整数 Number. It performs the following steps when called:
令 inLeapYear 为 InLeapYear (t )。 令 dayWithinYear 为 DayWithinYear (t )。 令 month 为 MonthFromTime (t )。 若 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 ( t )
The abstract operation WeekDay takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 6 𝔽 (含)的整数 Number. It performs the following steps when called:
返回 𝔽 (ℝ (Day (t ) + 4 𝔽 ) 模 7)。
21.4.1.14 HourFromTime ( t )
The abstract operation HourFromTime takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 23 𝔽 (含)的整数 Number. It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerHour )) 模 HoursPerDay )。
21.4.1.15 MinFromTime ( t )
The abstract operation MinFromTime takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 59 𝔽 (含)的整数 Number. It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerMinute )) 模 MinutesPerHour )。
21.4.1.16 SecFromTime ( t )
The abstract operation SecFromTime takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 59 𝔽 (含)的整数 Number. It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerSecond )) 模 SecondsPerMinute )。
21.4.1.17 msFromTime ( t )
The abstract operation msFromTime takes argument t (有限 时间值 ) and returns 区间 +0 𝔽 至 999 𝔽 (含)的整数 Number. It performs the following steps when called:
返回 𝔽 (ℝ (t ) 模 ℝ (msPerSecond ))。
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 BigInt. 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 区间 组成的字符串。
ECMAScript 支持的时区可以是 可用命名时区 ,其由 AvailableNamedTimeZoneIdentifiers 返回的时区标识符记录 的 [[Identifier]] 字段表示,或是 偏移时区 ,其由 IsTimeZoneOffsetString 返回 true 的字符串表示。
主时区标识符 是可用命名时区的首选标识符。
非主时区标识符 是可用命名时区的非主标识符。
可用命名时区标识符 是主时区标识符或非主时区标识符中的一种。
每个可用命名时区标识符只关联一个可用命名时区。
每个可用命名时区只关联一个主时区标识符和零个或多个非主时区标识符。
ECMAScript 实现必须支持标识符为 "UTC" 的可用命名时区,并且该标识符必须是 UTC 时区的主时区标识符。
此外,实现可以支持任意数量的其他可用命名时区。
遵循 ECMA-402 国际化 API 规范描述的时区要求的实现称为 具备时区感知 。
具备时区感知的实现必须支持与 IANA 时区数据库的 Zone 和 Link 名称相对应的可用命名时区,并且仅支持这些名称。
在具备时区感知的实现中,主时区标识符是 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 (字符串), 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 BigInt 列表.
当输入表示由于负时区变更(如夏令时结束或时区规则调整导致偏移减少)而某本地时间出现多次时,返回的列表会有多个元素,并按数值升序排序。
当输入表示由于正时区变更(如夏令时开始或时区规则调整导致偏移增加)而某本地时间被跳过时,返回的列表为空。
否则返回的列表只有一个元素。
未包含任何时区本地规则的 ECMAScript 实现的默认实现步骤如下:
断言:timeZoneIdentifier 为 "UTC" 。 令 epochNanoseconds 为 GetUTCEpochNanoseconds (year , month , day , hour , minute , second , millisecond , microsecond , nanosecond )。 返回 « epochNanoseconds »。
Note
时区感知实现(也推荐所有实现)需使用 IANA 时区数据库 https://www.iana.org/time-zones/ 的时区信息。
2017 年 11 月 5 日 1:30 AM 在 America/New_York 出现两次,因此 GetNamedTimeZoneEpochNanoseconds("America/New_York" , 2017, 11, 5, 1, 30, 0, 0, 0, 0) 会返回一个长度为 2 的列表,第一项表示 05:30 UTC(对应美国东部夏令时 UTC-04:00),第二项表示 06:30 UTC(对应美国东部标准时 UTC-05:00)。
2017 年 3 月 12 日 2:30 AM 在 America/New_York 不存在,因此 GetNamedTimeZoneEpochNanoseconds("America/New_York" , 2017, 3, 12, 2, 30, 0, 0, 0, 0) 会返回空列表。
21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier , epochNanoseconds )
The implementation-defined abstract operation GetNamedTimeZoneOffsetNanoseconds takes arguments timeZoneIdentifier (字符串) and epochNanoseconds (BigInt) and returns 整数.
返回的整数表示由 timeZoneIdentifier 标识的命名时区,在与 epochNanoseconds 相对应的时刻相对纪元 的 UTC 偏移(单位为纳秒)。
未包含任何时区本地规则的 ECMAScript 实现的默认实现步骤如下:
断言:timeZoneIdentifier 为 "UTC" 。 返回 0。
Note
21.4.1.22 时区标识符记录
时区标识符记录 是用于描述可用命名时区标识符 及其对应主时区标识符 的记录。
时区标识符记录具有 Table 62 中列出的字段。
Table 62: 时区标识符记录 字段
字段名
值
含义
[[Identifier]]
字符串
实现支持的可用命名时区标识符 。
[[PrimaryIdentifier]]
字符串
[[Identifier]] 解析到的主时区标识符 。
Note
若 [[Identifier]] 是主时区标识符 ,则 [[Identifier]] 即 [[PrimaryIdentifier]] 。
21.4.1.23 AvailableNamedTimeZoneIdentifiers ( )
The implementation-defined abstract operation AvailableNamedTimeZoneIdentifiers takes no arguments and returns 时区标识符记录 列表.
具备时区感知 的实现(包括所有实现 ECMA-402 国际化 API 的实现)必须按 ECMA-402 规范实现 AvailableNamedTimeZoneIdentifiers 抽象操作 。
对于不具备时区感知 的实现,AvailableNamedTimeZoneIdentifiers 的默认步骤如下:
如果实现未包含任何时区本地规则,返回 « 时区标识符记录 { [[Identifier]] : "UTC" , [[PrimaryIdentifier]] : "UTC" } »。 令 identifiers 为唯一可用命名时区标识符 列表,按字典码元顺序排序。 令 result 为新空列表。 对每个 identifier 元素,令 primary 为 identifier 。 如果 identifier 是此实现的非主时区标识符 且不是 "UTC" ,将 primary 设为与 identifier 关联的主时区标识符 。 注:实现可能需迭代解析 identifier 以获得主时区标识符 。 令 record 为时区标识符记录 { [[Identifier]] : identifier , [[PrimaryIdentifier]] : primary }。 将 record 添加到 result 。 断言:result 包含时区标识符记录 r ,使得 r .[[Identifier]] 为 "UTC" 且 r .[[PrimaryIdentifier]] 为 "UTC" 。 返回 result 。
21.4.1.24 SystemTimeZoneIdentifier ( )
The implementation-defined abstract operation SystemTimeZoneIdentifier takes no arguments and returns 字符串. It performs the following steps when called:
如果实现仅支持 UTC 时区,返回 "UTC" 。 令 systemTimeZoneString 为代表宿主环境 当前时区的字符串(主时区标识符 或偏移时区 标识符)。 返回 systemTimeZoneString 。
Note
为确保实现常见 Date 对象方法的功能,建议 SystemTimeZoneIdentifier 返回宿主环境 时区设置对应的 IANA 时区名(如存在)。
GetNamedTimeZoneEpochNanoseconds 和 GetNamedTimeZoneOffsetNanoseconds 必须反映该时区标准时间和夏令时的本地政治规则(如有)。
例如,若宿主环境 为设置为美国东部时间的浏览器,则 SystemTimeZoneIdentifier 返回 "America/New_York" 。
21.4.1.25 LocalTime ( t )
The abstract operation LocalTime takes argument t (有限 时间值 ) and returns 整数 Number. It performs the following steps when called:
令 systemTimeZoneIdentifier 为 SystemTimeZoneIdentifier ()。 如果 IsTimeZoneOffsetString (systemTimeZoneIdentifier ) 为 true ,令 offsetNs 为 ParseTimeZoneOffsetString (systemTimeZoneIdentifier )。 否则,令 offsetNs 为 GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier , ℤ (ℝ (t ) × 106 ))。 令 offsetMs 为 truncate (offsetNs / 106 )。 返回 t + 𝔽 (offsetMs )。
Note 1
Note 2
时区感知实现(也推荐所有实现)需使用 IANA 时区数据库 https://www.iana.org/time-zones/ 的时区信息。
Note 3
在负时区变更(如夏令时结束或时区调整减小时)发生重复时间时,两个不同输入时间值 t UTC 会被转换为同一本地时间 tlocal 。LocalTime(UTC (t local )) 不一定等于 t local ,同理 UTC (LocalTime(t UTC )) 也不一定等于 t UTC 。
21.4.1.26 UTC ( t )
The abstract operation UTC takes argument t (Number) and returns 时间值 . 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 表示在正时区变更时被跳过的本地时间(如夏令时开始或时区调整增大)。 令 possibleInstantsBefore 为 GetNamedTimeZoneEpochNanoseconds (systemTimeZoneIdentifier , ℝ (YearFromTime (tBefore )), ℝ (MonthFromTime (tBefore )) + 1, ℝ (DateFromTime (tBefore )), ℝ (HourFromTime (tBefore )), ℝ (MinFromTime (tBefore )), ℝ (SecFromTime (tBefore )), ℝ (msFromTime (tBefore )), 0, 0),其中 tBefore 为小于 t 的最大整数 Number,且 possibleInstantsBefore 非空(即 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 时区数据库 https://www.iana.org/time-zones/ 的时区信息。
2017 年 11 月 5 日 1:30 AM 在 America/New_York 出现两次(回拨),但应按 UTC-04 而不是 UTC-05 解释。
在 UTC(TimeClip (MakeDate (MakeDay (2017, 10, 5), MakeTime (1, 30, 0, 0)))) 中,offsetMs 的值为 -4 × msPerHour 。
2017 年 3 月 12 日 2:30 AM 在 America/New_York 不存在,但应按 UTC-05(等价于 UTC-04 的 3:30 AM)解释。
在 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. 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)。 找到 YearFromTime (t ) 为 ym 、MonthFromTime (t ) 为 mn 、DateFromTime (t ) 为 1 𝔽 的有限 时间值 t ;若不可行(如参数越界),返回 NaN 。 返回 Day (t ) + dt - 1 𝔽 。
21.4.1.29 MakeDate ( day , time )
The abstract operation MakeDate takes arguments day (Number) and time (Number) and returns Number. 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 . 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 Number. 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(1月)到 12(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
也包括“日期-时间”形式,即上述日期形式紧接以下时间形式之一并可选加偏移:
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 扩展年份
覆盖 约 273,790 年前后(自 1970 年 1 月 1 日起,见 21.4.1.1 )的完整时间值 范围需表示 0 年之前或 9999 年之后的年份。ISO 8601 允许年份扩展,但仅限信息交换双方协商采用。简化的 ECMAScript 格式规定扩展年份为 6 位数字,前缀为 + 或 -。年份 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 定义了用于 UTC 偏移的字符串交换格式,源自 ISO 8601。
格式描述如下语法。
语法
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 (字符串) and returns 布尔值. 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 (字符串) and returns 整数. It performs the following steps when called:
令 parseResult 为 ParseText(offsetString , UTCOffset )。 断言:parseResult 非错误列表。 断言:parseResult 包含 ASCIISign 解析节点 。 令 parsedSign 为 parseResult 内 ASCIISign 解析节点 匹配的源文本。 若 parsedSign 为 U+002D(连字符),令 sign 为 -1。 否则,令 sign 为 1。 注:下列 StringToNumber 应用不会丢精度,因为每个解析值均为短数字字符串。 断言:parseResult 包含 Hour 解析节点 。 令 parsedHours 为 parseResult 内 Hour 解析节点 匹配的源文本。 令 hours 为 ℝ (StringToNumber (CodePointsToString(parsedHours )))。 若 parseResult 不含 MinuteSecond 解析节点 ,令 minutes 为 0。 否则,令 parsedMinutes 为 parseResult 内第一个 MinuteSecond 解析节点 匹配的源文本。 令 minutes 为 ℝ (StringToNumber (CodePointsToString(parsedMinutes )))。 若 parseResult 不含两个 MinuteSecond 解析节点 ,令 seconds 为 0。 否则,令 parsedSeconds 为 parseResult 内第二个 MinuteSecond 解析节点 匹配的源文本。 令 seconds 为 ℝ (StringToNumber (CodePointsToString(parsedSeconds )))。 若 parseResult 不含 TemporalDecimalFraction 解析节点 ,令 nanoseconds 为 0。 否则,令 parsedFraction 为 parseResult 内 TemporalDecimalFraction 解析节点 匹配的源文本。 令 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 ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 DateFromTime (LocalTime (t ))。
21.4.4.3 Date.prototype.getDay ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 WeekDay (LocalTime (t ))。
21.4.4.4 Date.prototype.getFullYear ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 YearFromTime (LocalTime (t ))。
21.4.4.5 Date.prototype.getHours ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 HourFromTime (LocalTime (t ))。
21.4.4.6 Date.prototype.getMilliseconds ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 msFromTime (LocalTime (t ))。
21.4.4.7 Date.prototype.getMinutes ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 MinFromTime (LocalTime (t ))。
21.4.4.8 Date.prototype.getMonth ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 MonthFromTime (LocalTime (t ))。
21.4.4.9 Date.prototype.getSeconds ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 SecFromTime (LocalTime (t ))。
21.4.4.10 Date.prototype.getTime ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 返回 dateObject .[[DateValue]] 。
21.4.4.11 Date.prototype.getTimezoneOffset ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 (t - LocalTime (t )) / msPerMinute 。
21.4.4.12 Date.prototype.getUTCDate ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 DateFromTime (t )。
21.4.4.13 Date.prototype.getUTCDay ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 WeekDay (t )。
21.4.4.14 Date.prototype.getUTCFullYear ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 YearFromTime (t )。
21.4.4.15 Date.prototype.getUTCHours ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 HourFromTime (t )。
21.4.4.16 Date.prototype.getUTCMilliseconds ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 msFromTime (t )。
21.4.4.17 Date.prototype.getUTCMinutes ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 MinFromTime (t )。
21.4.4.18 Date.prototype.getUTCMonth ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 MonthFromTime (t )。
21.4.4.19 Date.prototype.getUTCSeconds ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,返回 NaN 。 返回 SecFromTime (t )。
21.4.4.20 Date.prototype.setDate ( date )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 dt 为 ? ToNumber (date )。 如果 t 为 NaN ,返回 NaN 。 设置 t 为 LocalTime (t )。 令 newDate 为 MakeDate (MakeDay (YearFromTime (t ), MonthFromTime (t ), dt ), TimeWithinDay (t ))。 令 u 为 TimeClip (UTC (newDate ))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
21.4.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 y 为 ? ToNumber (year )。 如果 t 为 NaN ,设置 t 为 +0 𝔽 ;否则设置 t 为 LocalTime (t )。 如果 month 未传入,令 m 为 MonthFromTime (t );否则令 m 为 ? ToNumber (month )。 如果 date 未传入,令 dt 为 DateFromTime (t );否则令 dt 为 ? ToNumber (date )。 令 newDate 为 MakeDate (MakeDay (y , m , dt ), TimeWithinDay (t ))。 令 u 为 TimeClip (UTC (newDate ))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
该方法的 "length" 属性为 3 𝔽 。
Note
如果未传入 month ,该方法表现为 month 取 getMonth()
的值。如果未传入 date ,则表现为 date 取 getDate()
的值。
21.4.4.22 Date.prototype.setHours ( hour [ , min [ , sec [ , ms ] ] ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 h 为 ? ToNumber (hour )。 如果传入 min ,令 m 为 ? ToNumber (min )。 如果传入 sec ,令 s 为 ? ToNumber (sec )。 如果传入 ms ,令 milli 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 设置 t 为 LocalTime (t )。 如果 min 未传入,令 m 为 MinFromTime (t )。 如果 sec 未传入,令 s 为 SecFromTime (t )。 如果 ms 未传入,令 milli 为 msFromTime (t )。 令 date 为 MakeDate (Day (t ), MakeTime (h , m , s , milli ))。 令 u 为 TimeClip (UTC (date ))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
该方法的 "length" 属性为 4 𝔽 。
Note
如果未传入 min ,该方法表现为 min 取 getMinutes()
的值。如果未传入 sec ,表现为 sec 取 getSeconds()
的值。如果未传入 ms ,表现为 ms 取 getMilliseconds()
的值。
21.4.4.23 Date.prototype.setMilliseconds ( ms )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 设置 ms 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 设置 t 为 LocalTime (t )。 令 time 为 MakeTime (HourFromTime (t ), MinFromTime (t ), SecFromTime (t ), ms )。 令 u 为 TimeClip (UTC (MakeDate (Day (t ), time )))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
21.4.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 m 为 ? ToNumber (min )。 如果传入 sec ,令 s 为 ? ToNumber (sec )。 如果传入 ms ,令 milli 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 设置 t 为 LocalTime (t )。 如果 sec 未传入,令 s 为 SecFromTime (t )。 如果 ms 未传入,令 milli 为 msFromTime (t )。 令 date 为 MakeDate (Day (t ), MakeTime (HourFromTime (t ), m , s , milli ))。 令 u 为 TimeClip (UTC (date ))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
该方法的 "length" 属性为 3 𝔽 。
Note
如果未传入 sec ,该方法表现为 sec 取 getSeconds()
的值。如果未传入 ms ,表现为 ms 取 getMilliseconds()
的值。
21.4.4.25 Date.prototype.setMonth ( month [ , date ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 m 为 ? ToNumber (month )。 如果传入 date ,令 dt 为 ? ToNumber (date )。 如果 t 为 NaN ,返回 NaN 。 设置 t 为 LocalTime (t )。 如果 date 未传入,令 dt 为 DateFromTime (t )。 令 newDate 为 MakeDate (MakeDay (YearFromTime (t ), m , dt ), TimeWithinDay (t ))。 令 u 为 TimeClip (UTC (newDate ))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
该方法的 "length" 属性为 2 𝔽 。
Note
如果未传入 date ,该方法表现为 date 取 getDate()
的值。
21.4.4.26 Date.prototype.setSeconds ( sec [ , ms ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 s 为 ? ToNumber (sec )。 如果传入 ms ,令 milli 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 设置 t 为 LocalTime (t )。 如果 ms 未传入,令 milli 为 msFromTime (t )。 令 date 为 MakeDate (Day (t ), MakeTime (HourFromTime (t ), MinFromTime (t ), s , milli ))。 令 u 为 TimeClip (UTC (date ))。 设置 dateObject .[[DateValue]] 为 u 。 返回 u 。
该方法的 "length" 属性为 2 𝔽 。
Note
如果未传入 ms ,该方法表现为 ms 取 getMilliseconds()
的值。
21.4.4.27 Date.prototype.setTime ( time )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 ? ToNumber (time )。 令 v 为 TimeClip (t )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
21.4.4.28 Date.prototype.setUTCDate ( date )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 dt 为 ? ToNumber (date )。 如果 t 为 NaN ,返回 NaN 。 令 newDate 为 MakeDate (MakeDay (YearFromTime (t ), MonthFromTime (t ), dt ), TimeWithinDay (t ))。 令 v 为 TimeClip (newDate )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
21.4.4.29 Date.prototype.setUTCFullYear ( year [ , month [ , date ] ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 如果 t 为 NaN ,设置 t 为 +0 𝔽 。 令 y 为 ? ToNumber (year )。 如果 month 未传入,令 m 为 MonthFromTime (t );否则令 m 为 ? ToNumber (month )。 如果 date 未传入,令 dt 为 DateFromTime (t );否则令 dt 为 ? ToNumber (date )。 令 newDate 为 MakeDate (MakeDay (y , m , dt ), TimeWithinDay (t ))。 令 v 为 TimeClip (newDate )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
该方法的 "length" 属性为 3 𝔽 。
Note
如果未传入 month ,该方法表现为 month 取 getUTCMonth()
的值。如果未传入 date ,则表现为 date 取 getUTCDate()
的值。
21.4.4.30 Date.prototype.setUTCHours ( hour [ , min [ , sec [ , ms ] ] ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 h 为 ? ToNumber (hour )。 如果传入 min ,令 m 为 ? ToNumber (min )。 如果传入 sec ,令 s 为 ? ToNumber (sec )。 如果传入 ms ,令 milli 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 如果 min 未传入,令 m 为 MinFromTime (t )。 如果 sec 未传入,令 s 为 SecFromTime (t )。 如果 ms 未传入,令 milli 为 msFromTime (t )。 令 date 为 MakeDate (Day (t ), MakeTime (h , m , s , milli ))。 令 v 为 TimeClip (date )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
该方法的 "length" 属性为 4 𝔽 。
Note
如果未传入 min ,该方法表现为 min 取 getUTCMinutes()
的值。如果未传入 sec ,表现为 sec 取 getUTCSeconds()
的值。如果未传入 ms ,表现为 ms 取 getUTCMilliseconds()
的值。
21.4.4.31 Date.prototype.setUTCMilliseconds ( ms )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 设置 ms 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 令 time 为 MakeTime (HourFromTime (t ), MinFromTime (t ), SecFromTime (t ), ms )。 令 v 为 TimeClip (MakeDate (Day (t ), time ))。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
21.4.4.32 Date.prototype.setUTCMinutes ( min [ , sec [ , ms ] ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 m 为 ? ToNumber (min )。 如果传入 sec ,令 s 为 ? ToNumber (sec )。 如果传入 ms ,令 milli 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 如果 sec 未传入,令 s 为 SecFromTime (t )。 如果 ms 未传入,令 milli 为 msFromTime (t )。 令 date 为 MakeDate (Day (t ), MakeTime (HourFromTime (t ), m , s , milli ))。 令 v 为 TimeClip (date )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
该方法的 "length" 属性为 3 𝔽 。
Note
如果未传入 sec ,该方法表现为 sec 取 getUTCSeconds()
的值。如果未传入 ms ,表现为 ms 取 getUTCMilliseconds()
的值。
21.4.4.33 Date.prototype.setUTCMonth ( month [ , date ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 m 为 ? ToNumber (month )。 如果传入 date ,令 dt 为 ? ToNumber (date )。 如果 t 为 NaN ,返回 NaN 。 如果 date 未传入,令 dt 为 DateFromTime (t )。 令 newDate 为 MakeDate (MakeDay (YearFromTime (t ), m , dt ), TimeWithinDay (t ))。 令 v 为 TimeClip (newDate )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
该方法的 "length" 属性为 2 𝔽 。
Note
如果未传入 date ,该方法表现为 date 取 getUTCDate()
的值。
21.4.4.34 Date.prototype.setUTCSeconds ( sec [ , ms ] )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 t 为 dateObject .[[DateValue]] 。 令 s 为 ? ToNumber (sec )。 如果传入 ms ,令 milli 为 ? ToNumber (ms )。 如果 t 为 NaN ,返回 NaN 。 如果 ms 未传入,令 milli 为 msFromTime (t )。 令 date 为 MakeDate (Day (t ), MakeTime (HourFromTime (t ), MinFromTime (t ), s , milli ))。 令 v 为 TimeClip (date )。 设置 dateObject .[[DateValue]] 为 v 。 返回 v 。
该方法的 "length" 属性为 2 𝔽 。
Note
如果未传入 ms ,该方法表现为 ms 取 getUTCMilliseconds()
的值。
21.4.4.35 Date.prototype.toDateString ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 tv 为 dateObject .[[DateValue]] 。 如果 tv 为 NaN ,返回 "Invalid Date" 。 令 t 为 LocalTime (tv )。 返回 DateString (t )。
21.4.4.36 Date.prototype.toISOString ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 tv 为 dateObject .[[DateValue]] 。 如果 tv 为 NaN ,抛出 RangeError 异常。 断言:tv 是整数类型的数字。 如果 tv 对应的年份无法在 Date Time String Format 中表示,抛出 RangeError 异常。 返回 tv 在 UTC 时间轴上的 Date Time String Format 字符串表示,包括所有格式元素和 UTC 偏移表示 "Z" 。
21.4.4.37 Date.prototype.toJSON ( key )
此方法为 JSON.stringify
(25.5.2 ) 提供 Date 的字符串表示。
调用此方法时,执行以下步骤:
令 O 为 ? ToObject (this 值)。 令 tv 为 ? ToPrimitive(O , number )。 如果 tv 是数字且不是有限 值,返回 null 。 返回 ? Invoke(O , "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,则使用以下规范:
此方法返回一个字符串值。字符串内容由实现定义,但旨在以主机环境当前区域设置的习惯用法,以便捷、易读的方式表示当前时区中的 Date 的“日期”部分。
此方法的可选参数含义在 ECMA-402 规范中定义;不支持 ECMA-402 的实现不得在这些参数位置使用其他含义。
21.4.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用以下规范:
此方法返回一个字符串值。字符串内容由实现定义,但旨在以主机环境当前区域设置的习惯用法,以便捷、易读的方式表示当前时区中的 Date。
此方法的可选参数含义在 ECMA-402 规范中定义;不支持 ECMA-402 的实现不得在这些参数位置使用其他含义。
21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用以下规范:
此方法返回一个字符串值。字符串内容由实现定义,但旨在以主机环境当前区域设置的习惯用法,以便捷、易读的方式表示当前时区中的 Date 的“时间”部分。
此方法的可选参数含义在 ECMA-402 规范中定义;不支持 ECMA-402 的实现不得在这些参数位置使用其他含义。
21.4.4.41 Date.prototype.toString ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 tv 为 dateObject .[[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 (一个数字,但不是 NaN ) and returns 一个字符串. 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(空格)、"GMT" 的字符串拼接结果。
21.4.4.41.2 DateString ( tv )
The abstract operation DateString takes argument tv (一个数字,但不是 NaN ) and returns 一个字符串. It performs the following steps when called:
令 weekday 为 Table 63 中 WeekDay (tv ) 的 Name。 令 month 为 Table 64 中 MonthFromTime (tv ) 的 Name。 令 day 为 ToZeroPaddedDecimalString (ℝ (DateFromTime (tv )), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv 为 +0 𝔽 或 yv > +0 𝔽 ,令 yearSign 为空字符串;否则令 yearSign 为 "-" 。 令 paddedYear 为 ToZeroPaddedDecimalString (abs (ℝ (yv )), 4)。 返回 weekday 、码元 0x0020(空格)、month 、码元 0x0020(空格)、day 、码元 0x0020(空格)、yearSign 、paddedYear 的字符串拼接结果。
Table 63: 一周中各天的名称
Number
Name
+0 𝔽
"Sun"
1 𝔽
"Mon"
2 𝔽
"Tue"
3 𝔽
"Wed"
4 𝔽
"Thu"
5 𝔽
"Fri"
6 𝔽
"Sat"
Table 64: 一年中各月的名称
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 (一个整数类型的数字) and returns 一个字符串. 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 为实现定义的字符串,可以为空字符串,也可以是码元 0x0020(空格)、码元 0x0028(左括号)、实现定义的时区名称和码元 0x0029(右括号)拼接的字符串。 返回 offsetSign 、offsetHour 、offsetMin 、tzName 的字符串拼接结果。
21.4.4.41.4 ToDateString ( tv )
The abstract operation ToDateString takes argument tv (一个整数类型的数字或 NaN ) and returns 一个字符串. It performs the following steps when called:
如果 tv 为 NaN ,返回 "Invalid Date" 。 令 t 为 LocalTime (tv )。 返回 DateString (t )、码元 0x0020(空格)、TimeString (t )、TimeZoneString (tv ) 的字符串拼接结果。
21.4.4.42 Date.prototype.toTimeString ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 tv 为 dateObject .[[DateValue]] 。 如果 tv 为 NaN ,返回 "Invalid Date" 。 令 t 为 LocalTime (tv )。 返回 TimeString (t ) 和 TimeZoneString (tv ) 的字符串拼接结果。
21.4.4.43 Date.prototype.toUTCString ( )
此方法返回一个字符串值,表示与 this 值对应的时间点。字符串格式基于 RFC 7231 的 "HTTP-date",并加以泛化以支持 ECMAScript Date 支持的完整时间范围。
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 令 tv 为 dateObject .[[DateValue]] 。 如果 tv 为 NaN ,返回 "Invalid Date" 。 令 weekday 为 Table 63 中 WeekDay (tv ) 的 Name。 令 month 为 Table 64 中 MonthFromTime (tv ) 的 Name。 令 day 为 ToZeroPaddedDecimalString (ℝ (DateFromTime (tv )), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv 为 +0 𝔽 或 yv > +0 𝔽 ,令 yearSign 为空字符串;否则令 yearSign 为 "-" 。 令 paddedYear 为 ToZeroPaddedDecimalString (abs (ℝ (yv )), 4)。 返回 weekday 、"," 、码元 0x0020(空格)、day 、码元 0x0020(空格)、month 、码元 0x0020(空格)、yearSign 、paddedYear 、码元 0x0020(空格)、TimeString (tv ) 的字符串拼接结果。
21.4.4.44 Date.prototype.valueOf ( )
调用此方法时,执行以下步骤:
令 dateObject 为 this 值。 执行 ? RequireInternalSlot (dateObject , [[DateValue]] )。 返回 dateObject .[[DateValue]] 。
21.4.4.45 Date.prototype [ %Symbol.toPrimitive% ] ( hint )
此方法由 ECMAScript 语言运算符调用,用于将 Date 转换为原始值。hint 的允许值为 "default" 、"number" 和 "string" 。Date 在所有内建 ECMAScript 对象中是独特的,因为它将 "default" 视为等同于 "string" ,而其他内建对象则将 "default" 视为等同于 "number" 。
调用此方法时,执行以下步骤:
令 O 为 this 值。 如果 O 不是对象,抛出 TypeError 异常。 如果 hint 是 "string" 或 "default" ,令 tryFirst 为 string 。 否则如果 hint 是 "number" ,令 tryFirst 为 number 。 否则,抛出 TypeError 异常。 返回 ? OrdinaryToPrimitive (O , tryFirst )。
此属性具有 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true } 属性。
该方法的 "name" 属性值为 "[Symbol.toPrimitive]" 。