21.4.1 Date 对象概述与抽象操作定义
下列抽象操作 作用于时间值 (在 21.4.1.1 中定义)。注意,在每种情况下,如果这些函数的任何参数为 NaN ,结果都将为 NaN 。
21.4.1.1 时间值与时间范围
ECMAScript 中的时间测量类似于 POSIX 中的时间测量,特别是在使用追溯(proleptic)格里高利历、以协调世界时 1970 年 1 月 1 日午夜为 纪元 、并且规定每一天恰好包含 86,400 秒(每秒为 1000 毫秒)的定义方面保持一致。
ECMAScript 的 时间值 是一个 Number,要么是表示以毫秒精度的时刻的有限 整数 ,要么是表示无特定时刻的 NaN 。如果时间值是 24 × 60 × 60 × 1000 = 86,400,000(即为某整数 d 的 86,400,000 × d )的倍数,则表示该时间值对应的时刻为自纪元 起经过 d 个完整 UTC 天后某天开始的瞬间(对于负的 d 则在纪元 之前)。每一个其它有限 时间值 t 相对于最大的前一个该类倍数的时间值 s 定义,表示在与 s 同一 UTC 日内但在其之后 (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 日午夜的 -100,000,000 天到 100,000,000 天。
表示 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 ( t )
The abstract operation Day takes argument t (a finite time value) and returns an integral Number. 返回 t 所在日的日序号。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerDay )))。
21.4.1.4 TimeWithinDay ( t )
The abstract operation TimeWithinDay takes argument t (a finite time value) and returns an integral Number in the interval from +0 𝔽 (inclusive) to msPerDay (exclusive). 返回 t 所在日自该日开始经过的毫秒数。 It performs the following steps when called:
返回 𝔽 (ℝ (t ) 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 )。 注意:在下列步骤中,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 (an integral Number) and returns a time value. 返回年份 y 开始时刻的时间值 。 It performs the following steps when called:
返回 msPerDay × DayFromYear (y )。
21.4.1.8 YearFromTime ( t )
The abstract operation YearFromTime takes argument t (a finite time value) and returns an integral Number. 返回 t 所在的年份。 It performs the following steps when called:
返回最大的整数 Number y (最接近 +∞),使得 TimeFromYear (y ) ≤ t 。
21.4.1.9 DayWithinYear ( t )
The abstract operation DayWithinYear takes argument t (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 (t ) - DayFromYear (YearFromTime (t ))。
21.4.1.10 InLeapYear ( t )
The abstract operation InLeapYear takes argument t (a finite time value) and returns +0 𝔽 or 1 𝔽 . 如果 t 在闰年内则返回 1 𝔽 ,否则返回 +0 𝔽 。 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 (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 11 𝔽 . 返回一个 Number,标识 t 所在的月份。月份值 +0 𝔽 表示一月;1 𝔽 表示二月;...;11 𝔽 表示十二月。注意 MonthFromTime(+0 𝔽 ) = +0 𝔽 ,对应于 1970-01-01(星期四)。 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 (a finite time value) and returns an integral Number in the inclusive interval from 1 𝔽 to 31 𝔽 . 返回 t 所在月的日数(日期)。 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 (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 6 𝔽 . 返回一个 Number,标识 t 所在的星期几。星期值 +0 𝔽 表示星期日;1 𝔽 表示星期一;...;6 𝔽 表示星期六。注意 WeekDay(+0 𝔽 ) = 4 𝔽 ,对应于 1970-01-01(星期四)。 It performs the following steps when called:
返回 𝔽 (ℝ (Day (t ) + 4 𝔽 ) modulo 7)。
21.4.1.14 HourFromTime ( t )
The abstract operation HourFromTime takes argument t (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 23 𝔽 . 返回 t 所在日的小时数(0-23)。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerHour )) modulo HoursPerDay )。
21.4.1.15 MinFromTime ( t )
The abstract operation MinFromTime takes argument t (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 59 𝔽 . 返回 t 所在小时的分钟数(0-59)。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerMinute )) modulo MinutesPerHour )。
21.4.1.16 SecFromTime ( t )
The abstract operation SecFromTime takes argument t (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 59 𝔽 . 返回 t 所在分钟的秒数(0-59)。 It performs the following steps when called:
返回 𝔽 (floor (ℝ (t / msPerSecond )) modulo SecondsPerMinute )。
21.4.1.17 msFromTime ( t )
The abstract operation msFromTime takes argument t (a finite time value) and returns an integral Number in the inclusive interval from +0 𝔽 to 999 𝔽 . 返回 t 所在秒的毫秒数(0-999)。 It performs the following steps when called:
返回 𝔽 (ℝ (t ) 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. 返回值表示自纪元 以来的纳秒数,该纳秒数对应于给定的 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 内组成。
ECMAScript 实现支持的时区可以是由 AvailableNamedTimeZoneIdentifiers 返回的时区标识记录的 [[Identifier]] 字段表示的 可用命名时区 ,或者是由 IsTimeZoneOffsetString 返回 true 的字符串表示的 偏移时区 。
主时区标识符 是可用命名时区的首选标识符。
非主时区标识符 是实现中某可用命名时区的非主标识符。
可用命名时区标识符 是主时区标识符或非主时区标识符。
每个可用命名时区标识符与恰好一个可用命名时区关联。
每个可用命名时区与恰好一个主时区标识符以及零个或多个非主时区标识符关联。
ECMAScript 实现必须支持标识符为 "UTC" 的可用命名时区,该标识符必须是 UTC 时区的主时区标识符。
此外,实现可以支持任意数量的其它可命名时区。
遵循 ECMA-402 国际化 API 所描述的时区要求的实现称为 时区感知 。
时区感知实现必须支持与 IANA 时区数据库的 Zone 和 Link 名称对应的可用命名时区,且仅支持这些名称。
在时区感知实现中,主时区标识符为 IANA 时区数据库中的 Zone 名称,非主时区标识符为 Link 名称,除非 AvailableNamedTimeZoneIdentifiers (在 ECMA-402 规范中)有具体覆盖 说明。
不支持整个 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 (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.
返回列表中的每个值都表示自纪元 以来的纳秒数,该纳秒数对应于给定的 ISO 8601 日历日期和以 timeZoneIdentifier 所标识的命名时区的壁钟时间。
当输入表示由于负时区转换(例如夏令时结束或由于时区规则变更导致的 UTC 偏移减少)而发生多次的本地时间时,返回的列表将包含多个元素并按升序排序。
当输入表示由于正时区转换(例如夏令时开始或时区规则变更导致 UTC 偏移增加)而被跳过的本地时间时,返回的列表将为空。
否则,返回的列表将只有一个元素。
默认的 GetNamedTimeZoneEpochNanoseconds 实现(供不包含任何本地政治时区规则的 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 日美国纽约时间 01:30(America/New_York)被重复两次,因此 GetNamedTimeZoneEpochNanoseconds("America/New_York" , 2017, 11, 5, 1, 30, 0, 0, 0, 0) 将返回长度为 2 的列表,第一元素表示 05:30 UTC(对应于夏令时偏移 -04:00 的 01:30),第二元素表示 06:30 UTC(对应于标准时偏移 -05:00 的 01:30)。
2017 年 3 月 12 日美国纽约时间 02:30 不存在,因此 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 (a String) and epochNanoseconds (a BigInt) and returns an integer.
返回的整数 表示在与 epochNanoseconds 对应的时刻相对于纪元 的纳秒数单位计的由 timeZoneIdentifier 所标识的命名时区相对于 UTC 的偏移量。
默认的 GetNamedTimeZoneOffsetNanoseconds 实现(供不包含任何本地政治时区规则的 ECMAScript 实现使用)在被调用时执行下列步骤:
断言:timeZoneIdentifier 为 "UTC" 。 返回 0。
Note
21.4.1.22 时区标识符记录
时区标识符记录 是用于描述可用命名时区标识符 及其对应主时区标识符 的记录。
时区标识符记录具有 Table 59 列出的字段。
Table 59: 时区标识符记录 字段
字段名
值
含义
[[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 .
其结果描述此实现中所有可用的命名时区标识符 ,以及对应的主时区标识符 。
列表按每个时区标识记录的 [[Identifier]] 字段排序。
时区感知 实现(包括实现 ECMA-402 国际化 API 的所有实现)必须按照 ECMA-402 规范实现 AvailableNamedTimeZoneIdentifiers 抽象操作 。
对于非时区感知 的实现,AvailableNamedTimeZoneIdentifiers 在被调用时执行下列步骤:
如果实现不包含任何时区的本地政治规则,则返回 « the Time Zone Identifier Record { [[Identifier]] : "UTC" , [[PrimaryIdentifier]] : "UTC" } »。 令 identifiers 为唯一可用命名时区标识符 的列表,按字典编码单元顺序排序。 令 result 为一个新的空列表。 对于 identifiers 的每个元素 identifier ,执行令 primary 为 identifier 。 如果 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 a String.
返回表示宿主环境 当前时区的字符串,该字符串要么是满足 IsTimeZoneOffsetString 返回 true 的 UTC 偏移字符串,要么是一个主时区标识符 。
It performs the following steps when called:
如果实现仅支持 UTC 时区,则返回 "UTC" 。 令 systemTimeZoneString 为表示宿主环境 当前时区的字符串,要么是主时区标识符 ,要么是偏移时区 标识符。 返回 systemTimeZoneString 。
Note
为确保 Date 对象方法中实现通常提供的功能,建议 SystemTimeZoneIdentifier 返回与宿主环境 时区设置对应的 IANA 时区名称(如果存在)。
GetNamedTimeZoneEpochNanoseconds 与 GetNamedTimeZoneOffsetNanoseconds 必须反映该时区的本地政治规则(标准时与夏令时),如果存在这样的规则。
例如,如果宿主环境 为浏览器且系统将用户时区设置为美东时区(US Eastern Time),则 SystemTimeZoneIdentifier 返回 "America/New_York" 。
21.4.1.25 LocalTime ( t )
The abstract operation LocalTime takes argument t (a finite time value) and returns an integral Number.
将 t 从 UTC 转换为本地时间。
应使用在 t 时刻生效的本地政治规则来确定结果,如本节所述。
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
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 (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 表示在负时区转换时(例如夏令时结束或时区规则变更导致 UTC 偏移减少)重复多次的本地时间或在正时区转换时被跳过的本地时间时,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 的最大整型 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 ,因为 SystemTimeZoneIdentifier 返回 "UTC" 且 GetNamedTimeZoneOffsetNanoseconds 返回 0。
Note 1
要求时区感知 实现(并建议所有其它实现)使用 IANA 时区数据库的时区信息 https://www.iana.org/time-zones/ 。
2017 年 11 月 5 日美国纽约时间 01:30 被重复两次(回退),但必须将其解释为 UTC-04 的 01:30 而不是 UTC-05 的 01:30。
在 UTC(TimeClip (MakeDate (MakeDay (2017, 10, 5), MakeTime (1, 30, 0, 0)))), offsetMs 的值为 -4 × msPerHour 。
2017 年 3 月 12 日美国纽约时间 02:30 不存在,但必须将其解释为 UTC-05 的 02:30(等价于 UTC-04 的 03: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)。 寻找一个有限 时间值 t ,使 YearFromTime (t ) 为 ym ,MonthFromTime (t ) 为 mn ,且 DateFromTime (t ) 为 1 𝔽 ;但如果这不可行(因为某些参数超出范围),则返回 NaN 。 返回 Day (t ) + 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 的四位十进制数字,或作为带有 '+' 或 '-' 前缀的 6 位扩展年份(参见 expanded year )。
-
字符 "-" (连字符)在字符串中字面出现两次。
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 的提前或滞后,属于用于指示本地时间偏离 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 年向前或向后 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 (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. 返回值为与字符串 offsetString 对应的 UTC 偏移量,以纳秒为单位的整数 。 It performs the following steps when called:
令 parseResult 为 ParseText (offsetString , UTCOffset )。 断言:parseResult 不是错误列表。 断言:parseResult 包含一个 ASCIISign 解析节点 。 令 parsedSign 为 ASCIISign 解析节点 所匹配的源文本 。 如果 parsedSign 为单个代码点 U+002D(HYPHEN-MINUS),则令 sign 为 -1。 否则,令 sign 为 1。 注意:下面对 StringToNumber 的应用不会丢失精度,因为每个被解析的值都保证是足够短的十进制数字字符串。 断言:parseResult 包含一个 Hour 解析节点 。 令 parsedHours 为 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 为 ? ToNumber (month );否则令 m 为 MonthFromTime (t )。 如果 date 存在,令 dt 为 ? ToNumber (date );否则令 dt 为 DateFromTime (t )。 令 newDate 为 MakeDate (MakeDay (y , m , dt ), TimeWithinDay (t ))。 令 u 为 TimeClip (UTC (newDate ))。 将 dateObject .[[DateValue]] 设为 u 。 返回 u 。
此方法的 "length" 属性为 3 𝔽 。
Note
如果未提供 month ,此方法的行为等同于将 month 视为 getMonth() 的值。如果未提供 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 ,此方法行为如同提供了 getMinutes() 的值。如果 未提供 sec ,行为如同提供了 getSeconds() 的值。如果 未提供 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 ,此方法行为如同提供了 getSeconds() 的值。如果 未提供 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 ,此方法行为如同提供了 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 ,此方法行为如同提供了 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 为 ? ToNumber (month );否则令 m 为 MonthFromTime (t )。 如果 date 存在,令 dt 为 ? ToNumber (date );否则令 dt 为 DateFromTime (t )。 令 newDate 为 MakeDate (MakeDay (y , m , dt ), TimeWithinDay (t ))。 令 v 为 TimeClip (newDate )。 将 dateObject .[[DateValue]] 设为 v 。 返回 v 。
此方法的 "length" 属性为 3 𝔽 。
Note
如果 未提供 month ,此方法行为如同提供了 getUTCMonth() 的值。如果 未提供 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 ,此方法行为如同提供了 getUTCMinutes() 的值。如果 未提供 sec ,行为如同提供了 getUTCSeconds() 的值。如果 未提供 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 ,此方法行为如同提供了 getUTCSeconds() 的值。如果 未提供 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 ,此方法行为如同提供了 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 ,此方法行为如同提供了 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 为整型 Number。 如果 tv 对应的年份不能在 日期时间字符串格式 中表示,抛出 RangeError 异常。 在 UTC 时间尺度上返回包含所有格式元素并附带 UTC 偏移表示 "Z" 的 tv 在 日期时间字符串格式 中的字符串表示。
21.4.4.37 Date.prototype.toJSON ( key )
此方法为 JSON.stringify (25.5.4 ) 提供 Date 的字符串表示。
调用时执行下列步骤:
令 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 规范实现此方法。如果实现不包含 ECMA-402 API,则使用以下对该方法的规范:
此方法返回一个字符串值。该字符串的内容由实现定义 ,但旨在以宿主环境 当前区域设置的惯例以一种方便的、可读的形式表示 Date 的“日期”部分(基于当前时区)。
此方法的可选参数意义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得将这些参数位置用于其他目的。
21.4.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范实现此方法。如果实现不包含 ECMA-402 API,则使用以下对该方法的规范:
此方法返回一个字符串值。该字符串的内容由实现定义 ,但旨在以宿主环境 当前区域设置的惯例以一种方便的、可读的形式表示 Date(基于当前时区)。
此方法的可选参数意义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得将这些参数位置用于其他目的。
21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] )
包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范实现此方法。如果实现不包含 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 (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 60 中与 Number WeekDay (tv ) 对应项的名称。 令 month 为表 Table 61 中与 Number MonthFromTime (tv ) 对应项的名称。 令 day 为 ToZeroPaddedDecimalString (ℝ (DateFromTime (tv )), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv 为 +0 𝔽 或 yv > +0 𝔽 ,令 yearSign 为空字符串;否则令 yearSign 为 "-" 。 令 paddedYear 为 ToZeroPaddedDecimalString (abs (ℝ (yv )), 4)。 返回 weekday 、代码单元 0x0020 (SPACE)、month 、代码单元 0x0020 (SPACE)、day 、代码单元 0x0020 (SPACE)、yearSign 与 paddedYear 的字符串连接 。
Table 60: 星期名称表
Number
Name
+0 𝔽
"Sun"
1 𝔽
"Mon"
2 𝔽
"Tue"
3 𝔽
"Wed"
4 𝔽
"Thu"
5 𝔽
"Fri"
6 𝔽
"Sat"
Table 61: 月份名称表
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 为一个实现定义 的字符串,该字符串要么为空,要么是代码单元 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" 。 令 t 为 LocalTime (tv )。 返回 DateString (t )、代码单元 0x0020 (SPACE)、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 60 中与 Number WeekDay (tv ) 对应项的名称。 令 month 为表 Table 61 中与 Number MonthFromTime (tv ) 对应项的名称。 令 day 为 ToZeroPaddedDecimalString (ℝ (DateFromTime (tv )), 2)。 令 yv 为 YearFromTime (tv )。 如果 yv 为 +0 𝔽 或 yv > +0 𝔽 ,令 yearSign 为空字符串;否则令 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 ( )
此方法在被调用时执行下列步骤:
令 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" 。
调用时执行下列步骤:
令 obj 为 this 值。 如果 obj 不是对象,抛出 TypeError 异常。 如果 hint 为 "string" 或 "default" ,则令 tryFirst 为 string 。 否则如果 hint 为 "number" ,则令 tryFirst 为 number 。 否则,抛出 TypeError 异常。 返回 ? OrdinaryToPrimitive (obj , tryFirst )。
此属性具有属性 { [[Writable]] : false , [[Enumerable]] : false , [[Configurable]] : true }。
此方法的 "name" 属性的值为 "[Symbol.toPrimitive]" 。