21 数字与日期

21.1 Number 对象

21.1.1 Number 构造函数

Number 构造函数:

  • %Number%
  • 全局对象"Number" 属性的初始值。
  • 当作为构造函数调用时,会创建并初始化一个新的 Number 对象。
  • 当作为函数而非构造函数调用时,会执行类型转换。
  • 可用作类定义的 extends 子句的值。打算继承指定 Number 行为的子类构造函数必须包含对 Number 构造函数的 super 调用,以创建并初始化具有 [[NumberData]] 内部槽的子类实例。

21.1.1.1 Number ( value )

调用此函数时执行下列步骤:

  1. 如果存在 value,则
    1. prim 为 ? ToNumeric(value)。
    2. 如果 prim 是 BigInt,令 n𝔽((prim))。
    3. 否则,令 nprim
  2. 否则,
    1. n+0𝔽
  3. 如果 NewTarget 为 undefined,则返回 n
  4. obj 为 ? OrdinaryCreateFromConstructor(NewTarget, "%Number.prototype%", « [[NumberData]] »)。
  5. obj.[[NumberData]] 设为 n
  6. 返回 obj

21.1.2 Number 构造函数的属性

Number 构造函数:

21.1.2.1 Number.EPSILON

Number.EPSILON 的值是表示 1 与可以作为 Number 值表示的比 1 稍大的最小值之间差值幅度的 Number 值,约为 2.2204460492503130808472633361816 × 10-16

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.2 Number.isFinite ( number )

调用此函数时执行下列步骤:

  1. 如果 number 不是 Number,返回 false
  2. 如果 number 不是有限值,返回 false
  3. 返回 true

21.1.2.3 Number.isInteger ( number )

调用此函数时执行下列步骤:

  1. 如果 number 是整型 Number,则返回 true
  2. 返回 false

21.1.2.4 Number.isNaN ( number )

调用此函数时执行下列步骤:

  1. 如果 number 不是 Number,返回 false
  2. 如果 numberNaN,返回 true
  3. 返回 false
Note

此函数不同于全局的 isNaN 函数(19.2.3),因为它在判断参数是否为 NaN 之前不会将其转换为 Number。

21.1.2.5 Number.isSafeInteger ( number )

Note

整数 n 当且仅当对应的 Number 值不等于任何其它整数Number 值时,称为“安全整数safe integer

调用此函数时执行下列步骤:

  1. 如果 number 是整型 Number,则
    1. 如果 abs((number)) ≤ 253 - 1,返回 true
  2. 返回 false

21.1.2.6 Number.MAX_SAFE_INTEGER

Note

由于 IEEE 754-2019 的精度限制导致的舍入行为,每个大于 Number.MAX_SAFE_INTEGER整数Number 值都与至少另一个整数共享相同的 Number 值。因此,此类大幅度整数并非 安全,不能保证能够被精确地表示为 Number 值,甚至不能保证彼此可区分。例如,90071992547409929007199254740993 的计算结果均为 Number 值 9007199254740992𝔽

Number.MAX_SAFE_INTEGER 的值为 9007199254740991𝔽 (𝔽(253 - 1))。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.7 Number.MAX_VALUE

Number.MAX_VALUE 的值是 Number 类型的最大正有限值,约为 1.7976931348623157 × 10308

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.8 Number.MIN_SAFE_INTEGER

Note

由于 IEEE 754-2019 的精度限制导致的舍入行为,每个小于 Number.MIN_SAFE_INTEGER整数Number 值都与至少另一个整数共享相同的 Number 值。因此,此类大幅度整数并非 安全,不能保证能够被精确地表示为 Number 值,甚至不能保证彼此可区分。例如,-9007199254740992-9007199254740993 的计算结果均为 Number 值 -9007199254740992𝔽

Number.MIN_SAFE_INTEGER 的值为 -9007199254740991𝔽 (𝔽(-(253 - 1)))。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.9 Number.MIN_VALUE

Number.MIN_VALUE 的值是 Number 类型的最小正值,约为 5 × 10-324

IEEE 754-2019 的双精度二进制表示中,最小可能值是一个非规格化(denormalized)数。如果实现不支持非规格化值,则 Number.MIN_VALUE 的值必须是实现实际能够表示的最小非零正值。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.10 Number.NaN

Number.NaN 的值是 NaN

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.11 Number.NEGATIVE_INFINITY

Number.NEGATIVE_INFINITY 的值是 -∞𝔽

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.12 Number.parseFloat ( string )

"parseFloat" 属性的初始值为 %parseFloat%

21.1.2.13 Number.parseInt ( string, radix )

"parseInt" 属性的初始值为 %parseInt%

21.1.2.14 Number.POSITIVE_INFINITY

Number.POSITIVE_INFINITY 的值是 +∞𝔽

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.2.15 Number.prototype

Number.prototype 的初始值为 Number 原型对象

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.1.3 Number 原型对象的属性

Number 原型对象

  • %Number.prototype%
  • 是一个普通对象
  • 自身是一个 Number 对象;它具有一个值为 +0𝔽[[NumberData]] 内部槽。
  • 有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%

除非另有明确说明,下面定义的 Number 原型对象的方法不是通用的,传递给它们的 this 值必须是一个 Number 值或一个已将 [[NumberData]] 内部槽初始化为 Number 值的对象。

在方法规范中短语“this Number value”指的是通过调用抽象操作 ThisNumberValue 并将方法调用的 this 值作为参数传入时返回的结果。

21.1.3.1 Number.prototype.constructor

Number.prototype.constructor 的初始值为 %Number%

21.1.3.2 Number.prototype.toExponential ( fractionDigits )

此方法返回一个字符串,其中包含该 Number 值以十进制指数记法表示的形式:在有效数字的小数点前有 1 位数字,并在小数点后有 fractionDigits 位数字。如果 fractionDigitsundefined,则包含足够的有效数字以唯一指定该 Number(类似 ToString,但在此情况下一律以指数表示法输出 Number)。

调用时执行下列步骤:

  1. numberValue 为 ? ThisNumberValue(this value)。
  2. fractionCount 为 ? ToIntegerOrInfinity(fractionDigits)。
  3. 断言:如果 fractionDigitsundefined,则 fractionCount 为 0。
  4. 如果 numberValue 不是有限值,返回 Number::toString(numberValue, 10)。
  5. 如果 fractionCount < 0 或 fractionCount > 100,抛出 RangeError 异常。
  6. numberValue 设为 (numberValue)。
  7. sign 为空字符串。
  8. 如果 numberValue < 0,则
    1. sign 设为 "-"
    2. numberValue 设为 -numberValue
  9. 如果 numberValue = 0,则
    1. significand 为由 fractionCount + 1 个代码单元 0x0030 (DIGIT ZERO) 组成的字符串值。
    2. exponent 为 0。
  10. 否则,
    1. 如果 fractionDigits 不是 undefined,则
      1. exponentintSignificand 为满足 10fractionCountintSignificand < 10fractionCount + 1 且使 intSignificand × 10exponent - fractionCount - numberValue 尽可能接近零的整数。如果存在两组这样的 exponentintSignificand,选择使 intSignificand × 10exponent - fractionCount 更大的那组 exponentintSignificand
    2. 否则,
      1. exponentintSignificandff 为满足 ff ≥ 0、10ffintSignificand < 10ff + 1𝔽(intSignificand × 10exponent - ff) 为 𝔽(numberValue) 且 ff 尽可能小的整数。注意 intSignificand 的十进制表示具有 ff + 1 位数,intSignificand 不可被 10 整除,并且 intSignificand 的最低有效位不一定可由这些条件唯一确定。
      2. fractionCount 设为 ff
    3. significand 为由 intSignificand 的十进制表示的数字(按顺序,无前导零)组成的字符串值。
  11. 如果 fractionCount ≠ 0,则
    1. integerPartsignificand 的第一个代码单元。
    2. fractionalPartsignificand 的其余 fractionCount 个代码单元。
    3. significand 设为 integerPart"."fractionalPart字符串连接
  12. 如果 exponent = 0,则
    1. exponentSign"+"
    2. exponentDigits"0"
  13. 否则,
    1. 如果 exponent > 0,则
      1. exponentSign"+"
    2. 否则,
      1. 断言:exponent < 0。
      2. exponentSign"-"
      3. exponent 设为 -exponent
    3. exponentDigits 为由 exponent 的十进制表示的数字(按顺序,无前导零)组成的字符串值。
  14. significand 设为 significand"e"exponentSignexponentDigits字符串连接
  15. 返回 signsignificand字符串连接
Note

对于提供比上述规则更精确转换的实现,建议(但标准不规定)在作为指引的情况下使用如下替代的第 10.b.i 步的版本:

  1. exponentintSignificandff 为满足 ff ≥ 0、10ffintSignificand < 10ff + 1𝔽(intSignificand × 10exponent - ff) 为 𝔽(numberValue) 且 ff 尽可能小的整数。如果对于 intSignificand 存在多种可能性,选择使 𝔽(intSignificand × 10exponent - ff) 最接近 𝔽(numberValue) 的 intSignificand。如果存在两个此类可能的 intSignificand 值,则选择偶数值。

21.1.3.3 Number.prototype.toFixed ( fractionDigits )

Note 1

此方法返回一个字符串,其中包含该 Number 值以十进制定点记法表示的形式,在小数点后有 fractionDigits 位。如果 fractionDigitsundefined,则假定为 0。

调用时执行下列步骤:

  1. numberValue 为 ? ThisNumberValue(this value)。
  2. fractionCount 为 ? ToIntegerOrInfinity(fractionDigits)。
  3. 断言:如果 fractionDigitsundefined,则 fractionCount 为 0。
  4. 如果 fractionCount 不是有限值,抛出 RangeError 异常。
  5. 如果 fractionCount < 0 或 fractionCount > 100,抛出 RangeError 异常。
  6. 如果 numberValue 不是有限值,返回 Number::toString(numberValue, 10)。
  7. numberValue 设为 (numberValue)。
  8. sign 为空字符串。
  9. 如果 numberValue < 0,则
    1. sign 设为 "-"
    2. numberValue 设为 -numberValue
  10. 如果 numberValue ≥ 1021,则
    1. digitString 为 ! ToString(𝔽(numberValue))。
  11. 否则,
    1. intValue 为一个整数,使 intValue / 10fractionCount - numberValue 尽可能接近零。如果有两个这样的 intValue,选择较大的 intValue
    2. 如果 intValue = 0,则令 digitString"0";否则令 digitString 为由 intValue 的十进制表示的数字组成的字符串值(按顺序,无前导零)。
    3. 如果 fractionCount ≠ 0,则
      1. digitCountdigitString 的长度。
      2. 如果 digitCountfractionCount,则
        1. zeroPad 为由 fractionCount + 1 - digitCount 个代码单元 0x0030 (DIGIT ZERO) 组成的字符串值。
        2. digitString 设为 zeroPaddigitString字符串连接
        3. digitCount 设为 fractionCount + 1。
      3. integerPartdigitString 的前 digitCount - fractionCount 个代码单元。
      4. fractionalPartdigitString 的其余 fractionCount 个代码单元。
      5. digitString 设为 integerPart"."fractionalPart字符串连接
  12. 返回 signdigitString字符串连接
Note 2

对于某些值,toFixed 的输出可能比 toString 更精确,因为 toString 只打印足以将该数与相邻的 Number 值区分开的有效数字。例如,

(1000000000000000128).toString() 返回 "1000000000000000100", while
(1000000000000000128).toFixed(0) returns "1000000000000000128".

21.1.3.4 Number.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )

包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范实现此方法。如果实现不包含 ECMA-402 API,则使用以下对该方法的规范:

此方法生成一个字符串值,表示根据宿主环境当前区域设置约定格式化的该 Number 值。该方法由实现定义,并且允许(但不鼓励)返回与 toString 相同的内容。

对于此方法的可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得将这些参数位置用于其他目的。

21.1.3.5 Number.prototype.toPrecision ( precision )

此方法返回一个字符串,其中包含该 Number 值表示为十进制指数记法(在有效数字的小数点前有一位且小数点后有 precision - 1 位)或以十进制定点记法表示为具有 precision 个有效数字的形式。如果 precisionundefined,则调用 ToString

调用时执行下列步骤:

  1. numberValue 为 ? ThisNumberValue(this value)。
  2. 如果 precisionundefined,返回 ! ToString(numberValue)。
  3. precisionCount 为 ? ToIntegerOrInfinity(precision)。
  4. 如果 numberValue 不是有限值,返回 Number::toString(numberValue, 10)。
  5. 如果 precisionCount < 1 或 precisionCount > 100,抛出 RangeError 异常。
  6. numberValue 设为 (numberValue)。
  7. sign 为空字符串。
  8. 如果 numberValue < 0,则
    1. sign 设为代码单元 0x002D (HYPHEN-MINUS)。
    2. numberValue 设为 -numberValue
  9. 如果 numberValue = 0,则
    1. significand 为由 precisionCount 个代码单元 0x0030 (DIGIT ZERO) 组成的字符串值。
    2. exponent 为 0。
  10. 否则,
    1. exponentintSignificand 为满足 10precisionCount - 1intSignificand < 10precisionCount 且使 intSignificand × 10exponent - precisionCount + 1 - numberValue 尽可能接近零的整数。如果存在两组这样的 exponentintSignificand,选择使 intSignificand × 10exponent - precisionCount + 1 更大的那组 exponentintSignificand
    2. significand 为由 intSignificand 的十进制表示的数字(按顺序,无前导零)组成的字符串值。
    3. 如果 exponent < -6 或 exponentprecisionCount,则
      1. 断言:exponent ≠ 0。
      2. 如果 precisionCount ≠ 1,则
        1. integerPartsignificand 的第一个代码单元。
        2. fractionalPartsignificand 的其余 precisionCount - 1 个代码单元。
        3. significand 设为 integerPart"."fractionalPart字符串连接
      3. 如果 exponent > 0,则
        1. exponentSign 为代码单元 0x002B (PLUS SIGN)。
      4. 否则,
        1. 断言:exponent < 0。
        2. exponentSign 为代码单元 0x002D (HYPHEN-MINUS)。
        3. exponent 设为 -exponent
      5. exponentDigits 为由 exponent 的十进制表示的数字(按顺序,无前导零)组成的字符串值。
      6. 返回 signsignificand、代码单元 0x0065 (LATIN SMALL LETTER E)、exponentSignexponentDigits字符串连接
  11. 如果 exponent = precisionCount - 1,返回 signsignificand字符串连接
  12. 如果 exponent ≥ 0,则
    1. significand 设为由 significand 的前 exponent + 1 个代码单元、代码单元 0x002E (FULL STOP) 和剩余的 precisionCount - (exponent + 1) 个代码单元组成的字符串连接
  13. 否则,
    1. significand 设为代码单元 0x0030 (DIGIT ZERO)、代码单元 0x002E (FULL STOP)、-(exponent + 1) 个代码单元 0x0030 (DIGIT ZERO) 以及字符串 significand字符串连接
  14. 返回 signsignificand字符串连接

21.1.3.6 Number.prototype.toString ( [ radix ] )

Note

可选的 radix 应为包含在闭区间 2𝔽36𝔽 内的整数 Number 值。如果 radixundefined,则使用 10𝔽 作为 radix 的值。

调用此方法时执行下列步骤:

  1. x 为 ? ThisNumberValue(this value)。
  2. 如果 radixundefined,令 radixMV 为 10。
  3. 否则,令 radixMV 为 ? ToIntegerOrInfinity(radix)。
  4. 如果 radixMV 不在 2 到 36 的闭区间内,抛出 RangeError 异常。
  5. 返回 Number::toString(x, radixMV)。

此方法不是通用的;如果其 this 值既不是 Number 也不是 Number 对象,则抛出 TypeError 异常。因此,不能将其转移给其他类型的对象来作为方法使用。

此方法的 "length" 属性为 1𝔽

21.1.3.7 Number.prototype.valueOf ( )

  1. 返回 ? ThisNumberValue(this value)。

21.1.3.7.1 ThisNumberValue ( value )

The abstract operation ThisNumberValue takes argument value (an ECMAScript language value) and returns either a normal completion containing a Number or a throw completion. It performs the following steps when called:

  1. 如果 value 是一个 Number,返回 value
  2. 如果 value 是一个对象且 value 拥有 [[NumberData]] 内部槽,则
    1. nvalue.[[NumberData]]
    2. 断言:n 是一个 Number。
    3. 返回 n
  3. 抛出 TypeError 异常。

21.1.4 Number 实例的属性

Number 实例是普通对象,从 Number 原型对象继承属性。Number 实例也具有一个 [[NumberData]] 内部槽。[[NumberData]] 内部槽是该 Number 对象所表示的 Number 值

21.2 BigInt 对象

21.2.1 BigInt 构造函数

BigInt 构造函数:

  • %BigInt%
  • 全局对象"BigInt" 属性的初始值。
  • 当作为函数而非构造函数调用时执行类型转换。
  • 不应与 new 操作符一起使用,也不应被子类化。它可用作类定义的 extends 子句的值,但对其的 super 调用会导致异常。

21.2.1.1 BigInt ( value )

调用此函数时执行下列步骤:

  1. 如果 NewTarget 不是 undefined,抛出 TypeError 异常。
  2. prim 为 ? ToPrimitive(value, number)。
  3. 如果 prim 是一个 Number,返回 ? NumberToBigInt(prim)。
  4. 返回 ? ToBigInt(prim)。

21.2.1.1.1 NumberToBigInt ( number )

The abstract operation NumberToBigInt takes argument number (a Number) and returns either a normal completion containing a BigInt or a throw completion. It performs the following steps when called:

  1. 如果 number 不是整型 Number,抛出 RangeError 异常。
  2. 返回 ((number))。

21.2.2 BigInt 构造函数的属性

BigInt 构造函数:

21.2.2.1 BigInt.asIntN ( bits, bigint )

调用此函数时执行下列步骤:

  1. bits 设为 ? ToIndex(bits)。
  2. bigint 设为 ? ToBigInt(bigint)。
  3. mod(bigint) 模 2bits
  4. 如果 mod ≥ 2bits - 1,返回 (mod - 2bits)。
  5. 返回 (mod)。

21.2.2.2 BigInt.asUintN ( bits, bigint )

调用此函数时执行下列步骤:

  1. bits 设为 ? ToIndex(bits)。
  2. bigint 设为 ? ToBigInt(bigint)。
  3. 返回 ((bigint) 模 2bits)。

21.2.2.3 BigInt.prototype

BigInt.prototype 的初始值为 BigInt 原型对象

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.2.3 BigInt 原型对象的属性

BigInt 原型对象

  • %BigInt.prototype%
  • 是一个普通对象
  • 不是 BigInt 对象;它不具有 [[BigIntData]] 内部槽。
  • 有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%

在方法规范中短语“this BigInt value”指的是通过调用抽象操作 ThisBigIntValue 并将方法调用的 this 值作为参数传入时返回的结果。

21.2.3.1 BigInt.prototype.constructor

BigInt.prototype.constructor 的初始值为 %BigInt%

21.2.3.2 BigInt.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )

包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范实现此方法。如果实现不包含 ECMA-402 API,则使用以下对该方法的规范:

此方法生成一个字符串值,表示根据宿主环境当前区域设置约定格式化的该 BigInt 值。该方法由实现定义,并且允许(但不鼓励)返回与 toString 相同的内容。

对于此方法的可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得将这些参数位置用于其他目的。

21.2.3.3 BigInt.prototype.toString ( [ radix ] )

Note

可选的 radix 应为包含在闭区间 2𝔽36𝔽 内的整数 Number 值。如果 radixundefined,则使用 10𝔽 作为 radix 的值。

调用此方法时执行下列步骤:

  1. x 为 ? ThisBigIntValue(this value)。
  2. 如果 radixundefined,令 radixMV 为 10。
  3. 否则,令 radixMV 为 ? ToIntegerOrInfinity(radix)。
  4. 如果 radixMV 不在 2 到 36 的闭区间内,抛出 RangeError 异常。
  5. 返回 BigInt::toString(x, radixMV)。

此方法不是通用的;如果其 this 值既不是 BigInt 也不是 BigInt 对象,则抛出 TypeError 异常。因此,不能将其转移给其他类型的对象来作为方法使用。

21.2.3.4 BigInt.prototype.valueOf ( )

  1. 返回 ? ThisBigIntValue(this value)。

21.2.3.4.1 ThisBigIntValue ( value )

The abstract operation ThisBigIntValue takes argument value (an ECMAScript language value) and returns either a normal completion containing a BigInt or a throw completion. It performs the following steps when called:

  1. 如果 value 是一个 BigInt,返回 value
  2. 如果 value 是一个对象且 value 拥有 [[BigIntData]] 内部槽,则
    1. 断言:value.[[BigIntData]] 是一个 BigInt。
    2. 返回 value.[[BigIntData]]
  3. 抛出 TypeError 异常。

21.2.3.5 BigInt.prototype [ %Symbol.toStringTag% ]

%Symbol.toStringTag% 属性的初始值为字符串 "BigInt"

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

21.2.4 BigInt 实例的属性

BigInt 实例是普通对象,从 BigInt 原型对象继承属性。BigInt 实例也具有一个 [[BigIntData]] 内部槽。[[BigIntData]] 内部槽是该 BigInt 对象所表示的 BigInt 值

21.3 Math 对象

Math 对象:

  • %Math%
  • 全局对象"Math" 属性的初始值。
  • 是一个普通对象
  • 有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%
  • 不是一个函数对象
  • 没有 [[Construct]] 内部方法;不能用 new 操作符作为构造函数使用。
  • 没有 [[Call]] 内部方法;不能被当作函数调用。
Note

在本规范中,短语“x 的 Number 值”具有在 6.1.6.1 中定义的技术含义。

21.3.1 Math 对象的数值属性

21.3.1.1 Math.E

自然对数的底数 e 的 Number 值,约为 2.7182818284590452354。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.3.1.2 Math.LN10

10 的自然对数的 Number 值,约为 2.302585092994046。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.3.1.3 Math.LN2

2 的自然对数的 Number 值,约为 0.6931471805599453。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.3.1.4 Math.LOG10E

e 的以 10 为底的对数的 Number 值;该值约为 0.4342944819032518。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

Note

Math.LOG10E 的值约等于 Math.LN10 值的倒数。

21.3.1.5 Math.LOG2E

e 的以 2 为底的对数的 Number 值;该值约为 1.4426950408889634。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

Note

Math.LOG2E 的值约等于 Math.LN2 值的倒数。

21.3.1.6 Math.PI

圆周与直径之比 π 的 Number 值,约为 3.1415926535897932。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.3.1.7 Math.SQRT1_2

½ 的平方根的 Number 值,约为 0.7071067811865476。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

Note

Math.SQRT1_2 的值约等于 Math.SQRT2 值的倒数。

21.3.1.8 Math.SQRT2

2 的平方根的 Number 值,约为 1.4142135623730951。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.3.1.9 Math [ %Symbol.toStringTag% ]

%Symbol.toStringTag% 属性的初始值为字符串 "Math"

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

21.3.2 Math 对象的函数属性

Note

函数 acos, acosh, asin, asinh, atan, atanh, atan2, cbrt, cos, cosh, exp, expm1, hypot, log, log1p, log2, log10, pow, random, sin, sinh, tan, 和 tanh 的行为在此并未被精确规定,仅要求在一些表示边界情况的参数值上产生特定结果。对于其他参数值,这些函数旨在计算熟悉的数学函数的近似值,但在近似算法的选择上允许具有一定的自由度。总体意图是实现者应当能够在给定硬件平台上对 ECMAScript 使用与该平台上为 C 程序员可用的相同数学库。

虽然算法的选择留给实现,但建议(标准不强制)实现使用 Sun Microsystems 提供的可自由分发数学库 fdlibm 中针对 IEEE 754-2019 算术的近似算法(http://www.netlib.org/fdlibm)。

21.3.2.1 Math.abs ( x )

此函数返回 x 的绝对值;结果具有与 x 相同的幅度但为正号。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN,返回 NaN
  3. 如果 n-0𝔽,返回 +0𝔽
  4. 如果 n-∞𝔽,返回 +∞𝔽
  5. 如果 n < -0𝔽,返回 -n
  6. 返回 n

21.3.2.2 Math.acos ( x )

此函数返回 x 的反余弦。结果以弧度表示,并位于闭区间+0𝔽𝔽(π) 之内。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaNn > 1𝔽,或 n < -1𝔽,返回 NaN
  3. 如果 n1𝔽,返回 +0𝔽
  4. 返回表示 (n) 的反余弦的实现近似的 Number 值

21.3.2.3 Math.acosh ( x )

此函数返回 x 的反双曲余弦。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+∞𝔽,返回 n
  3. 如果 n1𝔽,返回 +0𝔽
  4. 如果 n < 1𝔽,返回 NaN
  5. 返回表示 (n) 的反双曲余弦的实现近似的 Number 值

21.3.2.4 Math.asin ( x )

此函数返回 x 的反正弦。结果以弧度表示,并位于闭区间𝔽(-π / 2) 到 𝔽(π / 2) 之内。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n > 1𝔽n < -1𝔽,返回 NaN
  4. 返回表示 (n) 的反正弦的实现近似的 Number 值

21.3.2.5 Math.asinh ( x )

此函数返回 x 的反双曲正弦。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n+0𝔽-0𝔽,则返回 n
  3. 返回表示 (n) 的反双曲正弦的实现近似的 Number 值

21.3.2.6 Math.atan ( x )

此函数返回 x 的反正切。结果以弧度表示,并位于闭区间𝔽(-π / 2) 到 𝔽(π / 2) 之内。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n+∞𝔽,返回表示 π / 2 的实现近似的 Number 值
  4. 如果 n-∞𝔽,返回表示 -π / 2 的实现近似的 Number 值
  5. 返回表示 (n) 的反正切的实现近似的 Number 值

21.3.2.7 Math.atanh ( x )

此函数返回 x 的反双曲正切。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n > 1𝔽n < -1𝔽,返回 NaN
  4. 如果 n1𝔽,返回 +∞𝔽
  5. 如果 n-1𝔽,返回 -∞𝔽
  6. 返回表示 (n) 的反双曲正切的实现近似的 Number 值

21.3.2.8 Math.atan2 ( y, x )

此函数返回参数 yx 的商 y / x 的反正切,其结果象限由 yx 的符号决定。注意,对于两参数反正切函数,习惯上并且有意将名为 y 的参数放在第一位,将名为 x 的参数放在第二位。结果以弧度表示,并位于包含 -π 到 +π 的闭区间内。

调用时执行下列步骤:

  1. ny 为 ? ToNumber(y)。
  2. nx 为 ? ToNumber(x)。
  3. 如果 nyNaNnxNaN,返回 NaN
  4. 如果 ny+∞𝔽,则
    1. 如果 nx+∞𝔽,返回表示 π / 4 的实现近似的 Number 值
    2. 如果 nx-∞𝔽,返回表示 3π / 4 的实现近似的 Number 值
    3. 返回表示 π / 2 的实现近似的 Number 值
  5. 如果 ny-∞𝔽,则
    1. 如果 nx+∞𝔽,返回表示 -π / 4 的实现近似的 Number 值
    2. 如果 nx-∞𝔽,返回表示 -3π / 4 的實现近似的 Number 值
    3. 返回表示 -π / 2 的实现近似的 Number 值
  6. 如果 ny+0𝔽,则
    1. 如果 nx > +0𝔽nx+0𝔽,返回 +0𝔽
    2. 返回表示 π 的实现近似的 Number 值
  7. 如果 ny-0𝔽,则
    1. 如果 nx > +0𝔽nx+0𝔽,返回 -0𝔽
    2. 返回表示 -π 的实现近似的 Number 值
  8. 断言:ny有限值并且既不是 +0𝔽 也不是 -0𝔽
  9. 如果 ny > +0𝔽,则
    1. 如果 nx+∞𝔽,返回 +0𝔽
    2. 如果 nx-∞𝔽,返回表示 π 的实现近似的 Number 值
    3. 如果 nx+0𝔽-0𝔽,返回表示 π / 2 的实现近似的 Number 值
  10. 如果 ny < -0𝔽,则
    1. 如果 nx+∞𝔽,返回 -0𝔽
    2. 如果 nx-∞𝔽,返回表示 -π 的实现近似的 Number 值
    3. 如果 nx+0𝔽-0𝔽,返回表示 -π / 2 的实现近似的 Number 值
  11. 断言:nx有限值并且既不是 +0𝔽 也不是 -0𝔽
  12. rabs((ny) / (nx)) 的反正切。
  13. 如果 nx < -0𝔽,则
    1. 如果 ny > +0𝔽,将 r 设为 π - r
    2. 否则,将 r 设为 -π + r
  14. 否则,
    1. 如果 ny < -0𝔽,将 r 设为 -r
  15. 返回表示 r 的实现近似的 Number 值

21.3.2.9 Math.cbrt ( x )

此函数返回 x 的立方根。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n+0𝔽-0𝔽,返回 n
  3. 返回表示 (n) 的立方根的实现近似的 Number 值

21.3.2.10 Math.ceil ( x )

此函数返回不小于 x 的最小(最接近 -∞)的整数 Number 值。如果 x 已经是整数 Number,则结果为 x

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n+0𝔽-0𝔽,返回 n
  3. 如果 n < -0𝔽n > -1𝔽,返回 -0𝔽
  4. 如果 n 是整型 Number,返回 n
  5. 返回不小于 n 的最小(最接近 -∞)的整数 Number 值。
Note

Math.ceil(x) 的值等于 -Math.floor(-x) 的值。

21.3.2.11 Math.clz32 ( x )

此函数在被调用时执行下列步骤:

  1. n 为 ? ToUint32(x) 的 表示。
  2. pn 的无符号 32 位二进制表示中的前导零位数。
  3. 返回 𝔽(p)。
Note

如果 n+0𝔽-0𝔽,此方法返回 32𝔽。如果 n 的 32 位二进制编码的最高有效位为 1,则此方法返回 +0𝔽

21.3.2.12 Math.cos ( x )

此函数返回 x 的余弦。参数以弧度表示。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值,返回 NaN
  3. 如果 n+0𝔽-0𝔽,返回 1𝔽
  4. 返回表示 (n) 的余弦的实现近似的 Number 值

21.3.2.13 Math.cosh ( x )

此函数返回 x 的双曲余弦。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN,返回 NaN
  3. 如果 n+∞𝔽-∞𝔽,返回 +∞𝔽
  4. 如果 n+0𝔽-0𝔽,返回 1𝔽
  5. 返回表示 (n) 的双曲余弦的实现近似的 Number 值
Note

Math.cosh(x) 的值等于 (Math.exp(x) + Math.exp(-x)) / 2 的值。

21.3.2.14 Math.exp ( x )

此函数返回 x 的指数函数(e 的 x 次方,其中 e 为自然对数的底数)。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+∞𝔽,返回 n
  3. 如果 n+0𝔽-0𝔽,返回 1𝔽
  4. 如果 n-∞𝔽,返回 +0𝔽
  5. 返回表示 (n) 的指数函数的实现近似的 Number 值

21.3.2.15 Math.expm1 ( x )

此函数返回指数函数减 1 的结果(e 的 x 次方减 1)。该结果以在 x 接近 0 时仍能保持准确的方式计算。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽+∞𝔽 之一,返回 n
  3. 如果 n-∞𝔽,返回 -1𝔽
  4. exp(n) 的指数函数。
  5. 返回表示 exp - 1 的实现近似的 Number 值

21.3.2.16 Math.floor ( x )

此函数返回不大于 x 的最大(最接近 +∞)的整数 Number 值。如果 x 已经是整数 Number,则结果为 x

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n+0𝔽-0𝔽,返回 n
  3. 如果 n < 1𝔽n > +0𝔽,返回 +0𝔽
  4. 如果 n 是整型 Number,返回 n
  5. 返回不大于 n 的最大(最接近 +∞)的整数 Number 值。
Note

Math.floor(x) 的值等于 -Math.ceil(-x) 的值。

21.3.2.17 Math.fround ( x )

此函数在被调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN,返回 NaN
  3. 如果 n+0𝔽-0𝔽+∞𝔽-∞𝔽 之一,返回 n
  4. n32 为使用 roundTiesToEven 模式将 n 转换为 IEEE 754-2019 binary32 格式的结果。
  5. n64 为将 n32 转换为 IEEE 754-2019 binary64 格式的结果。
  6. 返回与 n64 对应的 ECMAScript Number 值

21.3.2.18 Math.f16round ( x )

此函数在被调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN,返回 NaN
  3. 如果 n+0𝔽-0𝔽+∞𝔽-∞𝔽 之一,返回 n
  4. n16 为使用 roundTiesToEven 模式将 n 转换为 IEEE 754-2019 binary16 格式的结果。
  5. n64 为将 n16 转换为 IEEE 754-2019 binary64 格式的结果。
  6. 返回与 n64 对应的 ECMAScript Number 值
Note

此操作不同于先转为 binary32 然后再转为 binary16,因为可能发生双重舍入:例如,考虑数值 k = 1.00048828125000022204𝔽,对于该数 Math.f16round(k) 为 1.0009765625𝔽,但 Math.f16round(Math.fround(k)) 为 1𝔽

并非所有平台都提供从 binary64 到 binary16 的本地转换支持。有多种库可以提供此功能,包括 MIT 许可的 half 库。或者,也可以先在 roundTiesToEven 下将 binary64 转为 binary32,然后检查该结果是否可能导致不正确的双重舍入。此类情况可通过调整 binary32 值的尾数来显式处理,使其成为在 roundTiesToOdd 下执行初始转换时将产生的值。然后在 roundTiesToEven 下将调整后的值转换为 binary16 即可得到正确值。

21.3.2.19 Math.hypot ( ...args )

给定零个或多个参数,此函数返回其参数的平方和的平方根。

调用时执行下列步骤:

  1. coerced 为一个新的空列表。
  2. 对于 args 的每个元素 arg,执行
    1. n 为 ? ToNumber(arg)。
    2. n 附加到 coerced
  3. 对于 coerced 的每个元素 number,执行
    1. 如果 number+∞𝔽-∞𝔽,返回 +∞𝔽
  4. onlyZerotrue
  5. 对于 coerced 的每个元素 number,执行
    1. 如果 numberNaN,返回 NaN
    2. 如果 number 既不是 +0𝔽 也不是 -0𝔽,将 onlyZero 设为 false
  6. 如果 onlyZerotrue,返回 +0𝔽
  7. 返回表示 coerced 中元素的数学值的平方和的平方根的实现近似的 Number 值

此函数的 "length" 属性为 2𝔽

Note

实现应注意避免在带有两个或更多参数的朴素实现中容易发生的溢出和下溢导致的精度丢失。

21.3.2.20 Math.imul ( x, y )

此函数在被调用时执行下列步骤:

  1. a(? ToUint32(x))。
  2. b(? ToUint32(y))。
  3. product 为 (a × b) 模 232
  4. 如果 product ≥ 231,返回 𝔽(product - 232)。
  5. 返回 𝔽(product)。

21.3.2.21 Math.log ( x )

此函数返回 x 的自然对数。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+∞𝔽,返回 n
  3. 如果 n1𝔽,返回 +0𝔽
  4. 如果 n+0𝔽-0𝔽,返回 -∞𝔽
  5. 如果 n < -0𝔽,返回 NaN
  6. 返回表示 ln((n)) 的实现近似的 Number 值

21.3.2.22 Math.log1p ( x )

此函数返回 1 + x 的自然对数。该结果以在 x 接近 0 时仍能保持准确的方式计算。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽+∞𝔽 之一,返回 n
  3. 如果 n-1𝔽,返回 -∞𝔽
  4. 如果 n < -1𝔽,返回 NaN
  5. 返回表示 ln(1 + (n)) 的实现近似的 Number 值

21.3.2.23 Math.log10 ( x )

此函数返回 x 的以 10 为底的对数。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+∞𝔽,返回 n
  3. 如果 n1𝔽,返回 +0𝔽
  4. 如果 n+0𝔽-0𝔽,返回 -∞𝔽
  5. 如果 n < -0𝔽,返回 NaN
  6. 返回表示 log10((n)) 的实现近似的 Number 值

21.3.2.24 Math.log2 ( x )

此函数返回 x 的以 2 为底的对数。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+∞𝔽,返回 n
  3. 如果 n1𝔽,返回 +0𝔽
  4. 如果 n+0𝔽-0𝔽,返回 -∞𝔽
  5. 如果 n < -0𝔽,返回 NaN
  6. 返回表示 log2((n)) 的实现近似的 Number 值

21.3.2.25 Math.max ( ...args )

给定零个或多个参数,此函数对每个参数调用 ToNumber 并返回所得值中的最大值。

调用时执行下列步骤:

  1. coerced 为一个新的空列表。
  2. 对于 args 的每个元素 arg,执行
    1. n 为 ? ToNumber(arg)。
    2. n 附加到 coerced
  3. highest-∞𝔽
  4. 对于 coerced 的每个元素 number,执行
    1. 如果 numberNaN,返回 NaN
    2. 如果 number+0𝔽highest-0𝔽,将 highest 设为 +0𝔽
    3. 如果 number > highest,将 highest 设为 number
  5. 返回 highest
Note

用于确定最大值的比较使用 IsLessThan 算法,但将 +0𝔽 视为大于 -0𝔽

此函数的 "length" 属性为 2𝔽

21.3.2.26 Math.min ( ...args )

给定零个或多个参数,此函数对每个参数调用 ToNumber 并返回所得值中的最小值。

调用时执行下列步骤:

  1. coerced 为一个新的空列表。
  2. 对于 args 的每个元素 arg,执行
    1. n 为 ? ToNumber(arg)。
    2. n 附加到 coerced
  3. lowest+∞𝔽
  4. 对于 coerced 的每个元素 number,执行
    1. 如果 numberNaN,返回 NaN
    2. 如果 number-0𝔽lowest+0𝔽,将 lowest 设为 -0𝔽
    3. 如果 number < lowest,将 lowest 设为 number
  5. 返回 lowest
Note

用于确定最小值的比较使用 IsLessThan 算法,但将 +0𝔽 视为大于 -0𝔽

此函数的 "length" 属性为 2𝔽

21.3.2.27 Math.pow ( base, exponent )

此函数在被调用时执行下列步骤:

  1. base 设为 ? ToNumber(base)。
  2. exponent 设为 ? ToNumber(exponent)。
  3. 返回 Number::exponentiate(base, exponent)。

21.3.2.28 Math.random ( )

此函数返回一个具有正号的 Number 值,>= +0𝔽 且严格小于 1𝔽,该值由实现定义的算法或策略以近似均匀分布随机或伪随机选择。

为不同 realm 创建的每个 Math.random 函数在连续调用时必须产生不同的值序列。

21.3.2.29 Math.round ( x )

此函数返回最接近且为整数Number 值。如果两个整数同样接近 x,结果为更接近 +∞ 的 Number 值。如果 x 已为整数,则结果为 x

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n 是整型 Number,返回 n
  3. 如果 n < 0.5𝔽n > +0𝔽,返回 +0𝔽
  4. 如果 n < -0𝔽n-0.5𝔽,返回 -0𝔽
  5. 返回最接近 n整数 Number 值;在平局情况下偏向接近 +∞ 的值。
Note 1

Math.round(3.5) 返回 4,但 Math.round(-3.5) 返回 -3。

Note 2

Math.round(x) 的值不总是等于 Math.floor(x + 0.5) 的值。当 x-0𝔽x 小于 -0𝔽 但大于等于 -0.5𝔽 时,Math.round(x) 返回 -0𝔽,而 Math.floor(x + 0.5) 返回 +0𝔽。在计算 x + 0.5 时的内部舍入也可能导致 Math.round(x)Math.floor(x + 0.5) 不同。

21.3.2.30 Math.sign ( x )

此函数返回 x 的符号,指示 x 为正、负或零。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n < -0𝔽,返回 -1𝔽
  4. 返回 1𝔽

21.3.2.31 Math.sin ( x )

此函数返回 x 的正弦。参数以弧度表示。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n+∞𝔽-∞𝔽,返回 NaN
  4. 返回表示 (n) 的正弦的实现近似的 Number 值

21.3.2.32 Math.sinh ( x )

此函数返回 x 的双曲正弦。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n+0𝔽-0𝔽,返回 n
  3. 返回表示 (n) 的双曲正弦的实现近似的 Number 值
Note

Math.sinh(x) 的值等于 (Math.exp(x) - Math.exp(-x)) / 2 的值。

21.3.2.33 Math.sqrt ( x )

此函数返回 x 的平方根。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽+∞𝔽 之一,返回 n
  3. 如果 n < -0𝔽,返回 NaN
  4. 返回 𝔽((n) 的平方根)。

21.3.2.34 Math.sumPrecise ( items )

给定一个 Number 的可迭代对象,此函数对可迭代对象中的每个值求和并返回它们的总和。如果任一值不是 Number,则抛出 TypeError 异常。

调用时执行下列步骤:

  1. 执行 ? RequireObjectCoercible(items)。
  2. iteratorRecord 为 ? GetIterator(items, sync)。
  3. stateminus-zero
  4. sum 为 0。
  5. count 为 0。
  6. nextnot-started
  7. 重复,直到 nextdone
    1. next 设为 ? IteratorStepValue(iteratorRecord)。
    2. 如果 next 不是 done,则
      1. 如果 count ≥ 253 - 1,则
        1. 注意:此步骤在实践中预计不会达到,仅包含以便实现可依赖输入为“合理大小”而不违反本规范。
        2. errorThrowCompletion(新创建的 RangeError 对象)。
        3. 返回 ? IteratorClose(iteratorRecord, error)。
      2. 如果 next 不是 Number,则
        1. errorThrowCompletion(新创建的 TypeError 对象)。
        2. 返回 ? IteratorClose(iteratorRecord, error)。
      3. nnext
      4. 如果 state 不是 not-a-number,则
        1. 如果 nNaN,则
          1. state 设为 not-a-number
        2. 否则如果 n+∞𝔽,则
          1. 如果 stateminus-infinity,将 state 设为 not-a-number
          2. 否则,将 state 设为 plus-infinity
        3. 否则如果 n-∞𝔽,则
          1. 如果 stateplus-infinity,将 state 设为 not-a-number
          2. 否则,将 state 设为 minus-infinity
        4. 否则如果 n 不是 -0𝔽stateminus-zerofinite,则
          1. state 设为 finite
          2. sum 设为 sum + (n)。
      5. count 设为 count + 1。
  8. 如果 statenot-a-number,返回 NaN
  9. 如果 stateplus-infinity,返回 +∞𝔽
  10. 如果 stateminus-infinity,返回 -∞𝔽
  11. 如果 stateminus-zero,返回 -0𝔽
  12. 返回 𝔽(sum)。
Note

可以通过多种算法在无需任意精度算术的情况下计算 sum 的值。一个此类算法是 Jonathan Richard Shewchuk 在 Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates 中给出的 "Grow-Expansion" 算法。一个较新的算法见 "Fast exact summation using small and large superaccumulators",其代码可在 https://gitlab.com/radfordneal/xsum 获取。

21.3.2.35 Math.tan ( x )

此函数返回 x 的正切。参数以弧度表示。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n+∞𝔽-∞𝔽,返回 NaN
  4. 返回表示 (n) 的正切的实现近似的 Number 值

21.3.2.36 Math.tanh ( x )

此函数返回 x 的双曲正切。

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 nNaN+0𝔽-0𝔽 之一,返回 n
  3. 如果 n+∞𝔽,返回 1𝔽
  4. 如果 n-∞𝔽,返回 -1𝔽
  5. 返回表示 (n) 的双曲正切的实现近似的 Number 值
Note

Math.tanh(x) 的值等于 (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x)) 的值。

21.3.2.37 Math.trunc ( x )

此函数返回数字 x整数部分,去除任何小数位。如果 x 已为整数,则结果为 x

调用时执行下列步骤:

  1. n 为 ? ToNumber(x)。
  2. 如果 n 不是有限值或 n+0𝔽-0𝔽,返回 n
  3. 如果 n < 1𝔽n > +0𝔽,返回 +0𝔽
  4. 如果 n < -0𝔽n > -1𝔽,返回 -0𝔽
  5. 返回朝向 +0𝔽 的最接近 n整数 Number 值。

21.4 Date 对象

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.821.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𝔽
msPerMinute = 60000𝔽 = msPerSecond × 𝔽(SecondsPerMinute)
msPerHour = 3600000𝔽 = msPerMinute × 𝔽(MinutesPerHour)
msPerDay = 86400000𝔽 = msPerHour × 𝔽(HoursPerDay)

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:

  1. 返回 𝔽(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:

  1. 返回 𝔽((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:

  1. ry(y)。
  2. 如果 (ry modulo 400) = 0,返回 366𝔽
  3. 如果 (ry modulo 100) = 0,返回 365𝔽
  4. 如果 (ry modulo 4) = 0,返回 366𝔽
  5. 返回 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:

  1. ry(y)。
  2. 注意:在下列步骤中,numYears1, numYears4, numYears100, 和 numYears400 表示从纪元到年份 y 开始之间可被 1、4、100、和 400 整除的年份数。若 y纪元之前,则该数为负。
  3. numYears1 为 (ry - 1970)。
  4. numYears4floor((ry - 1969) / 4)。
  5. numYears100floor((ry - 1901) / 100)。
  6. numYears400floor((ry - 1601) / 400)。
  7. 返回 𝔽(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:

  1. 返回 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:

  1. 返回最大的整数 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:

  1. 返回 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:

  1. 如果 DaysInYear(YearFromTime(t)) 为 366𝔽,返回 1𝔽
  2. 返回 +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:

  1. inLeapYearInLeapYear(t)。
  2. dayWithinYearDayWithinYear(t)。
  3. 如果 dayWithinYear < 31𝔽,返回 +0𝔽
  4. 如果 dayWithinYear < 59𝔽 + inLeapYear,返回 1𝔽
  5. 如果 dayWithinYear < 90𝔽 + inLeapYear,返回 2𝔽
  6. 如果 dayWithinYear < 120𝔽 + inLeapYear,返回 3𝔽
  7. 如果 dayWithinYear < 151𝔽 + inLeapYear,返回 4𝔽
  8. 如果 dayWithinYear < 181𝔽 + inLeapYear,返回 5𝔽
  9. 如果 dayWithinYear < 212𝔽 + inLeapYear,返回 6𝔽
  10. 如果 dayWithinYear < 243𝔽 + inLeapYear,返回 7𝔽
  11. 如果 dayWithinYear < 273𝔽 + inLeapYear,返回 8𝔽
  12. 如果 dayWithinYear < 304𝔽 + inLeapYear,返回 9𝔽
  13. 如果 dayWithinYear < 334𝔽 + inLeapYear,返回 10𝔽
  14. 断言:dayWithinYear < 365𝔽 + inLeapYear
  15. 返回 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:

  1. inLeapYearInLeapYear(t)。
  2. dayWithinYearDayWithinYear(t)。
  3. monthMonthFromTime(t)。
  4. 如果 month+0𝔽,返回 dayWithinYear + 1𝔽
  5. 如果 month1𝔽,返回 dayWithinYear - 30𝔽
  6. 如果 month2𝔽,返回 dayWithinYear - 58𝔽 - inLeapYear
  7. 如果 month3𝔽,返回 dayWithinYear - 89𝔽 - inLeapYear
  8. 如果 month4𝔽,返回 dayWithinYear - 119𝔽 - inLeapYear
  9. 如果 month5𝔽,返回 dayWithinYear - 150𝔽 - inLeapYear
  10. 如果 month6𝔽,返回 dayWithinYear - 180𝔽 - inLeapYear
  11. 如果 month7𝔽,返回 dayWithinYear - 211𝔽 - inLeapYear
  12. 如果 month8𝔽,返回 dayWithinYear - 242𝔽 - inLeapYear
  13. 如果 month9𝔽,返回 dayWithinYear - 272𝔽 - inLeapYear
  14. 如果 month10𝔽,返回 dayWithinYear - 303𝔽 - inLeapYear
  15. 断言:month11𝔽
  16. 返回 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:

  1. 返回 𝔽((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:

  1. 返回 𝔽(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:

  1. 返回 𝔽(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:

  1. 返回 𝔽(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:

  1. 返回 𝔽((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:

  1. dateMakeDay(𝔽(year), 𝔽(month - 1), 𝔽(day))。
  2. timeMakeTime(𝔽(hour), 𝔽(minute), 𝔽(second), 𝔽(millisecond))。
  3. msMakeDate(date, time)。
  4. 断言:ms 是整型 Number。
  5. 返回 ((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 实现使用)在被调用时执行下列步骤:

  1. 断言:timeZoneIdentifier"UTC"
  2. epochNanosecondsGetUTCEpochNanoseconds(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond)。
  3. 返回 « 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 实现使用)在被调用时执行下列步骤:

  1. 断言:timeZoneIdentifier"UTC"
  2. 返回 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 在被调用时执行下列步骤:

  1. 如果实现不包含任何时区的本地政治规则,则
    1. 返回 « the Time Zone Identifier Record { [[Identifier]]: "UTC", [[PrimaryIdentifier]]: "UTC" } »。
  2. identifiers 为唯一可用命名时区标识符的列表,按字典编码单元顺序排序。
  3. result 为一个新的空列表。
  4. 对于 identifiers 的每个元素 identifier,执行
    1. primaryidentifier
    2. 如果 identifier 在该实现中为非主时区标识符identifier 不是 "UTC",则
      1. primary 设为与 identifier 关联的主时区标识符
      2. 注意:实现可能需要迭代解析 identifier 以获得主时区标识符
    3. record时区标识符记录 { [[Identifier]]: identifier, [[PrimaryIdentifier]]: primary }。
    4. record 附加到 result
  5. 断言:result 包含一个时区标识符记录 r 使得 r.[[Identifier]]"UTC"r.[[PrimaryIdentifier]]"UTC"
  6. 返回 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:

  1. 如果实现仅支持 UTC 时区,则返回 "UTC"
  2. systemTimeZoneString 为表示宿主环境当前时区的字符串,要么是主时区标识符,要么是偏移时区标识符。
  3. 返回 systemTimeZoneString
Note

为确保 Date 对象方法中实现通常提供的功能,建议 SystemTimeZoneIdentifier 返回与宿主环境时区设置对应的 IANA 时区名称(如果存在)。 GetNamedTimeZoneEpochNanosecondsGetNamedTimeZoneOffsetNanoseconds 必须反映该时区的本地政治规则(标准时与夏令时),如果存在这样的规则。

例如,如果宿主环境为浏览器且系统将用户时区设置为美东时区(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:

  1. systemTimeZoneIdentifierSystemTimeZoneIdentifier()。
  2. 如果 IsTimeZoneOffsetString(systemTimeZoneIdentifier) 为 true,则
    1. offsetNsParseTimeZoneOffsetString(systemTimeZoneIdentifier)。
  3. 否则,
    1. offsetNsGetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, ((t) × 106))。
  4. offsetMstruncate(offsetNs / 106)。
  5. 返回 t + 𝔽(offsetMs)。
Note 1

如果实现中不存在本地时间的政治规则,结果为 t,因为 SystemTimeZoneIdentifier 返回 "UTC"GetNamedTimeZoneOffsetNanoseconds 返回 0。

Note 2

要求时区感知实现(并建议所有其它实现)使用 IANA 时区数据库的时区信息 https://www.iana.org/time-zones/

Note 3

在负时区转换发生重复时间(例如夏令时结束或时区调整减少)时,两个不同的输入时间值 tUTC 会转换为相同的本地时间 tlocal

LocalTime(UTC(tlocal)) 不必总是等于 tlocal。相应地,UTC(LocalTime(tUTC)) 也不必总是等于 tUTC

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:

  1. 如果 t 不是有限值,返回 NaN
  2. systemTimeZoneIdentifierSystemTimeZoneIdentifier()。
  3. 如果 IsTimeZoneOffsetString(systemTimeZoneIdentifier) 为 true,则
    1. offsetNsParseTimeZoneOffsetString(systemTimeZoneIdentifier)。
  4. 否则,
    1. possibleInstantsGetNamedTimeZoneEpochNanoseconds(systemTimeZoneIdentifier, (YearFromTime(t)), (MonthFromTime(t)) + 1, (DateFromTime(t)), (HourFromTime(t)), (MinFromTime(t)), (SecFromTime(t)), (msFromTime(t)), 0, 0)。
    2. 注意:下列步骤确保当 t 表示在负时区转换时(例如夏令时结束或时区规则变更导致 UTC 偏移减少)重复多次的本地时间或在正时区转换时被跳过的本地时间时,t 使用转换前的时区偏移来解释。
    3. 如果 possibleInstants 非空,则
      1. disambiguatedInstantpossibleInstants[0]。
    4. 否则,
      1. 注意:t 表示在正时区转换时被跳过的本地时间(例如由于夏令时开始或时区规则变更增加 UTC 偏移)。
      2. possibleInstantsBeforeGetNamedTimeZoneEpochNanoseconds(systemTimeZoneIdentifier, (YearFromTime(tBefore)), (MonthFromTime(tBefore)) + 1, (DateFromTime(tBefore)), (HourFromTime(tBefore)), (MinFromTime(tBefore)), (SecFromTime(tBefore)), (msFromTime(tBefore)), 0, 0),其中 tBefore 为小于 t 的最大整型 Number,使得 possibleInstantsBefore 非空(即 tBefore 表示转换前的最后一个本地时间)。
      3. disambiguatedInstantpossibleInstantsBefore 的最后一个元素。
    5. offsetNsGetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, disambiguatedInstant)。
  5. offsetMstruncate(offsetNs / 106)。
  6. 返回 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(tUTC)) 不必总是等于 tUTC。相应地,LocalTime(UTC(tlocal)) 不必总是等于 tlocal

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:

  1. 如果 hourminsecms 中有非有限值,返回 NaN
  2. h𝔽(! ToIntegerOrInfinity(hour))。
  3. m𝔽(! ToIntegerOrInfinity(min))。
  4. s𝔽(! ToIntegerOrInfinity(sec))。
  5. milli𝔽(! ToIntegerOrInfinity(ms))。
  6. 返回 ((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:

  1. 如果 yearmonthdate 中有非有限值,返回 NaN
  2. y𝔽(! ToIntegerOrInfinity(year))。
  3. m𝔽(! ToIntegerOrInfinity(month))。
  4. dt𝔽(! ToIntegerOrInfinity(date))。
  5. ymy + 𝔽(floor((m) / 12))。
  6. 如果 ym 不是有限值,返回 NaN
  7. mn𝔽((m) modulo 12)。
  8. 寻找一个有限时间值 t,使 YearFromTime(t) 为 ymMonthFromTime(t) 为 mn,且 DateFromTime(t) 为 1𝔽;但如果这不可行(因为某些参数超出范围),则返回 NaN
  9. 返回 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:

  1. 如果 daytime 不是有限值,返回 NaN
  2. tvday × msPerDay + time
  3. 如果 tv 不是有限值,返回 NaN
  4. 返回 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:

  1. 如果 yearNaN+∞𝔽-∞𝔽,返回 NaN
  2. truncated 为 ! ToIntegerOrInfinity(year)。
  3. 如果 truncated 在 0 到 99 的闭区间内,返回 1900𝔽 + 𝔽(truncated)。
  4. 返回 𝔽(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:

  1. 如果 time 不是有限值,返回 NaN
  2. 如果 abs((time)) > 8.64 × 1015,返回 NaN
  3. 返回 𝔽(! 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:0024:00 两种表示可用于区分与同一日期关联的两个午夜。这意味着下列两种表示指向完全相同的时刻:1995-02-04T24:001995-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 TemporalDecimalFractionopt 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:

  1. parseResultParseText(offsetString, UTCOffset)。
  2. 如果 parseResult 为错误列表,返回 false
  3. 返回 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:

  1. parseResultParseText(offsetString, UTCOffset)。
  2. 断言:parseResult 不是错误列表。
  3. 断言:parseResult 包含一个 ASCIISign 解析节点
  4. parsedSignASCIISign 解析节点匹配的源文本
  5. 如果 parsedSign 为单个代码点 U+002D(HYPHEN-MINUS),则
    1. sign 为 -1。
  6. 否则,
    1. sign 为 1。
  7. 注意:下面对 StringToNumber 的应用不会丢失精度,因为每个被解析的值都保证是足够短的十进制数字字符串。
  8. 断言:parseResult 包含一个 Hour 解析节点
  9. parsedHoursHour 解析节点匹配的源文本
  10. hours(StringToNumber(CodePointsToString(parsedHours)))。
  11. 如果 parseResult 不包含 MinuteSecond 解析节点,则
    1. minutes 为 0。
  12. 否则,
    1. parsedMinutesparseResult 中第一个 MinuteSecond 解析节点匹配的源文本
    2. minutes(StringToNumber(CodePointsToString(parsedMinutes))).
  13. 如果 parseResult 不包含两个 MinuteSecond 解析节点,则
    1. seconds 为 0。
  14. 否则,
    1. parsedSecondsparseResult 中第二个 MinuteSecond 解析节点匹配的源文本
    2. seconds(StringToNumber(CodePointsToString(parsedSeconds)))。
  15. 如果 parseResult 不包含 TemporalDecimalFraction 解析节点,则
    1. nanoseconds 为 0。
  16. 否则,
    1. parsedFractionparseResultTemporalDecimalFraction 解析节点匹配的源文本
    2. fractionCodePointsToString(parsedFraction) 与 "000000000"字符串连接
    3. nanosecondsStringfraction 的子串,从 1 到 10(含)。
    4. nanoseconds(StringToNumber(nanosecondsString))。
  17. 返回 sign × (((hours × 60 + minutes) × 60 + seconds) × 109 + nanoseconds)。

21.4.2 Date 构造函数

Date 构造函数:

  • %Date%
  • 全局对象"Date" 属性的初始值。
  • 当作为构造函数调用时,会创建并初始化一个新的 Date。
  • 当作为函数而非构造函数调用时,返回表示当前时间(UTC)的字符串。
  • 是一个函数,其行为取决于参数的数量和类型。
  • 可用作类定义的 extends 子句的值。打算继承指定 Date 行为的子类构造函数必须包含对 Date 构造函数的 super 调用,以创建并初始化具有 [[DateValue]] 内部槽的子类实例。

21.4.2.1 Date ( ...values )

调用此函数时执行下列步骤:

  1. 如果 NewTarget 为 undefined,则
    1. now 为标识当前时间的时间值(UTC)。
    2. 返回 ToDateString(now)。
  2. numberOfArgsvalues 中元素的数量。
  3. 如果 numberOfArgs = 0,则
    1. dv 为标识当前时间的时间值(UTC)。
  4. 否则如果 numberOfArgs = 1,则
    1. valuevalues[0]。
    2. 如果 value 是一个对象且 value 具有 [[DateValue]] 内部槽,则
      1. tvvalue.[[DateValue]]
    3. 否则,
      1. v 为 ? ToPrimitive(value)。
      2. 如果 v 是字符串,则
        1. 断言:下一步不会返回突发完成,因为 v 是字符串。
        2. tv 为将 v 解析为日期的结果,解析方式与 parse 方法(21.4.3.2)完全相同。
      3. 否则,
        1. tv 为 ? ToNumber(v)。
    4. dvTimeClip(tv)。
  5. 否则,
    1. 断言:numberOfArgs ≥ 2。
    2. y 为 ? ToNumber(values[0])。
    3. m 为 ? ToNumber(values[1])。
    4. 如果 numberOfArgs > 2,令 dt 为 ? ToNumber(values[2]);否则令 dt1𝔽
    5. 如果 numberOfArgs > 3,令 h 为 ? ToNumber(values[3]);否则令 h+0𝔽
    6. 如果 numberOfArgs > 4,令 min 为 ? ToNumber(values[4]);否则令 min+0𝔽
    7. 如果 numberOfArgs > 5,令 s 为 ? ToNumber(values[5]);否则令 s+0𝔽
    8. 如果 numberOfArgs > 6,令 milli 为 ? ToNumber(values[6]);否则令 milli+0𝔽
    9. yrMakeFullYear(y)。
    10. finalDateMakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))。
    11. dvTimeClip(UTC(finalDate))。
  6. obj 为 ? OrdinaryCreateFromConstructor(NewTarget, "%Date.prototype%", « [[DateValue]] »)。
  7. obj.[[DateValue]] 设为 dv
  8. 返回 obj

21.4.3 Date 构造函数的属性

Date 构造函数:

  • 具有一个 [[Prototype]] 内部槽,其值为 %Function.prototype%
  • 具有一个值为 7𝔽"length" 属性。
  • 具有下列属性:

21.4.3.1 Date.now ( )

此函数返回标示调用发生时 UTC 日期和时间的时间值

21.4.3.2 Date.parse ( string )

此函数对其参数应用 ToString 操作。如果 ToString 导致突发完成,则立即返回该完成记录。否则,此函数将所得字符串解释为日期和时间;它返回一个 Number,即与该日期和时间对应的 UTC 时间值。该字符串可被解释为本地时间、UTC 时间或某一时区中的时间,取决于字符串内容。函数首先尝试按日期时间字符串格式(21.4.1.32,包括扩展年)解析字符串。如果字符串不符合该格式,函数可回退到任何实现特定的启发式或日期格式。不可识别或包含超出格式元素值的字符串应使该函数返回 NaN

如果字符串符合 日期时间字符串格式,则缺失的格式元素由替代值代替。当缺失 MMDD 元素时,使用 "01"。当缺失 HHmmss 元素时,使用 "00"。当缺失 sss 元素时,使用 "000"。当缺失 UTC 偏移表示时,仅日期形式按 UTC 时间解释,而日期-时间形式按本地时间解释。

如果 x 为任何在特定 ECMAScript 实现中毫秒数为零的 Date,则在该实现中(若所有引用的属性都具有其初始值)下列表达式应产生相同的数值:

x.valueOf()
Date.parse(x.toString())
Date.parse(x.toUTCString())
Date.parse(x.toISOString())

然而,表达式

Date.parse(x.toLocaleString())

不要求产生与前面三者相同的 Number 值,并且一般而言,当给定任何不符合日期时间字符串格式(21.4.1.32)且不能由该实现的 toStringtoUTCString 方法生成的字符串值时,返回值为实现定义的。

21.4.3.3 Date.prototype

Date.prototype 的初始值为 Date 原型对象

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] )

调用此函数时执行下列步骤:

  1. y 为 ? ToNumber(year)。
  2. 如果 month 存在,令 m 为 ? ToNumber(month);否则令 m+0𝔽
  3. 如果 date 存在,令 dt 为 ? ToNumber(date);否则令 dt1𝔽
  4. 如果 hours 存在,令 h 为 ? ToNumber(hours);否则令 h+0𝔽
  5. 如果 minutes 存在,令 min 为 ? ToNumber(minutes);否则令 min+0𝔽
  6. 如果 seconds 存在,令 s 为 ? ToNumber(seconds);否则令 s+0𝔽
  7. 如果 ms 存在,令 milli 为 ? ToNumber(ms);否则令 milli+0𝔽
  8. yrMakeFullYear(y)。
  9. 返回 TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli)))。

此函数的 "length" 属性为 7𝔽

Note

此函数与 Date 构造函数有两点不同:它返回一个作为 Number 的时间值,而不是创建 Date;并且它将参数解释为 UTC 而非本地时间。

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 ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 DateFromTime(LocalTime(t))。

21.4.4.3 Date.prototype.getDay ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 WeekDay(LocalTime(t))。

21.4.4.4 Date.prototype.getFullYear ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 YearFromTime(LocalTime(t))。

21.4.4.5 Date.prototype.getHours ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 HourFromTime(LocalTime(t))。

21.4.4.6 Date.prototype.getMilliseconds ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 msFromTime(LocalTime(t))。

21.4.4.7 Date.prototype.getMinutes ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 MinFromTime(LocalTime(t))。

21.4.4.8 Date.prototype.getMonth ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 MonthFromTime(LocalTime(t))。

21.4.4.9 Date.prototype.getSeconds ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 SecFromTime(LocalTime(t))。

21.4.4.10 Date.prototype.getTime ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. 返回 dateObject.[[DateValue]]

21.4.4.11 Date.prototype.getTimezoneOffset ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 (t - LocalTime(t)) / msPerMinute

21.4.4.12 Date.prototype.getUTCDate ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 DateFromTime(t)。

21.4.4.13 Date.prototype.getUTCDay ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 WeekDay(t)。

21.4.4.14 Date.prototype.getUTCFullYear ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 YearFromTime(t)。

21.4.4.15 Date.prototype.getUTCHours ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 HourFromTime(t)。

21.4.4.16 Date.prototype.getUTCMilliseconds ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 msFromTime(t)。

21.4.4.17 Date.prototype.getUTCMinutes ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 MinFromTime(t)。

21.4.4.18 Date.prototype.getUTCMonth ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 MonthFromTime(t)。

21.4.4.19 Date.prototype.getUTCSeconds ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,返回 NaN
  5. 返回 SecFromTime(t)。

21.4.4.20 Date.prototype.setDate ( date )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. dt 为 ? ToNumber(date)。
  5. 如果 tNaN,返回 NaN
  6. t 设为 LocalTime(t)。
  7. newDateMakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t))。
  8. uTimeClip(UTC(newDate))。
  9. dateObject.[[DateValue]] 设为 u
  10. 返回 u

21.4.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. y 为 ? ToNumber(year)。
  5. 如果 tNaN,将 t 设为 +0𝔽;否则将 t 设为 LocalTime(t)。
  6. 如果 month 存在,令 m 为 ? ToNumber(month);否则令 mMonthFromTime(t)。
  7. 如果 date 存在,令 dt 为 ? ToNumber(date);否则令 dtDateFromTime(t)。
  8. newDateMakeDate(MakeDay(y, m, dt), TimeWithinDay(t))。
  9. uTimeClip(UTC(newDate))。
  10. dateObject.[[DateValue]] 设为 u
  11. 返回 u

此方法的 "length" 属性为 3𝔽

Note

如果未提供 month,此方法的行为等同于将 month 视为 getMonth() 的值。如果未提供 date,则视为 getDate() 的值。

21.4.4.22 Date.prototype.setHours ( hour [ , min [ , sec [ , ms ] ] ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. h 为 ? ToNumber(hour)。
  5. 如果 min 存在,令 m 为 ? ToNumber(min)。
  6. 如果 sec 存在,令 s 为 ? ToNumber(sec)。
  7. 如果 ms 存在,令 milli 为 ? ToNumber(ms)。
  8. 如果 tNaN,返回 NaN
  9. t 设为 LocalTime(t)。
  10. 如果 未提供 min,令 mMinFromTime(t)。
  11. 如果 未提供 sec,令 sSecFromTime(t)。
  12. 如果 未提供 ms,令 millimsFromTime(t)。
  13. dateMakeDate(Day(t), MakeTime(h, m, s, milli))。
  14. uTimeClip(UTC(date))。
  15. dateObject.[[DateValue]] 设为 u
  16. 返回 u

此方法的 "length" 属性为 4𝔽

Note

如果 未提供 min,此方法行为如同提供了 getMinutes() 的值。如果 未提供 sec,行为如同提供了 getSeconds() 的值。如果 未提供 ms,行为如同提供了 getMilliseconds() 的值。

21.4.4.23 Date.prototype.setMilliseconds ( ms )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. ms 设为 ? ToNumber(ms)。
  5. 如果 tNaN,返回 NaN
  6. t 设为 LocalTime(t)。
  7. timeMakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms)。
  8. uTimeClip(UTC(MakeDate(Day(t), time))).
  9. dateObject.[[DateValue]] 设为 u
  10. 返回 u

21.4.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. m 为 ? ToNumber(min)。
  5. 如果 sec 存在,令 s 为 ? ToNumber(sec)。
  6. 如果 ms 存在,令 milli 为 ? ToNumber(ms)。
  7. 如果 tNaN,返回 NaN
  8. t 设为 LocalTime(t)。
  9. 如果 未提供 sec,令 sSecFromTime(t)。
  10. 如果 未提供 ms,令 millimsFromTime(t)。
  11. dateMakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli))。
  12. uTimeClip(UTC(date))。
  13. dateObject.[[DateValue]] 设为 u
  14. 返回 u

此方法的 "length" 属性为 3𝔽

Note

如果 未提供 sec,此方法行为如同提供了 getSeconds() 的值。如果 未提供 ms,行为如同提供了 getMilliseconds() 的值。

21.4.4.25 Date.prototype.setMonth ( month [ , date ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. m 为 ? ToNumber(month)。
  5. 如果 date 存在,令 dt 为 ? ToNumber(date)。
  6. 如果 tNaN,返回 NaN
  7. t 设为 LocalTime(t)。
  8. 如果 未提供 date,令 dtDateFromTime(t)。
  9. newDateMakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t))。
  10. uTimeClip(UTC(newDate))。
  11. dateObject.[[DateValue]] 设为 u
  12. 返回 u

此方法的 "length" 属性为 2𝔽

Note

如果 未提供 date,此方法行为如同提供了 getDate() 的值。

21.4.4.26 Date.prototype.setSeconds ( sec [ , ms ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. s 为 ? ToNumber(sec)。
  5. 如果 ms 存在,令 milli 为 ? ToNumber(ms)。
  6. 如果 tNaN,返回 NaN
  7. t 设为 LocalTime(t)。
  8. 如果 未提供 ms,令 millimsFromTime(t)。
  9. dateMakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli))。
  10. uTimeClip(UTC(date))。
  11. dateObject.[[DateValue]] 设为 u
  12. 返回 u

此方法的 "length" 属性为 2𝔽

Note

如果 未提供 ms,此方法行为如同提供了 getMilliseconds() 的值。

21.4.4.27 Date.prototype.setTime ( time )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. t 为 ? ToNumber(time)。
  4. vTimeClip(t)。
  5. dateObject.[[DateValue]] 设为 v
  6. 返回 v

21.4.4.28 Date.prototype.setUTCDate ( date )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. dt 为 ? ToNumber(date)。
  5. 如果 tNaN,返回 NaN
  6. newDateMakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t))。
  7. vTimeClip(newDate)。
  8. dateObject.[[DateValue]] 设为 v
  9. 返回 v

21.4.4.29 Date.prototype.setUTCFullYear ( year [ , month [ , date ] ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. 如果 tNaN,将 t 设为 +0𝔽
  5. y 为 ? ToNumber(year)。
  6. 如果 month 存在,令 m 为 ? ToNumber(month);否则令 mMonthFromTime(t)。
  7. 如果 date 存在,令 dt 为 ? ToNumber(date);否则令 dtDateFromTime(t)。
  8. newDateMakeDate(MakeDay(y, m, dt), TimeWithinDay(t))。
  9. vTimeClip(newDate)。
  10. dateObject.[[DateValue]] 设为 v
  11. 返回 v

此方法的 "length" 属性为 3𝔽

Note

如果 未提供 month,此方法行为如同提供了 getUTCMonth() 的值。如果 未提供 date,行为如同提供了 getUTCDate() 的值。

21.4.4.30 Date.prototype.setUTCHours ( hour [ , min [ , sec [ , ms ] ] ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. h 为 ? ToNumber(hour)。
  5. 如果 min 存在,令 m 为 ? ToNumber(min)。
  6. 如果 sec 存在,令 s 为 ? ToNumber(sec)。
  7. 如果 ms 存在,令 milli 为 ? ToNumber(ms)。
  8. 如果 tNaN,返回 NaN
  9. 如果 未提供 min,令 mMinFromTime(t)。
  10. 如果 未提供 sec,令 sSecFromTime(t)。
  11. 如果 未提供 ms,令 millimsFromTime(t)。
  12. dateMakeDate(Day(t), MakeTime(h, m, s, milli))。
  13. vTimeClip(date)。
  14. dateObject.[[DateValue]] 设为 v
  15. 返回 v

此方法的 "length" 属性为 4𝔽

Note

如果 未提供 min,此方法行为如同提供了 getUTCMinutes() 的值。如果 未提供 sec,行为如同提供了 getUTCSeconds() 的值。如果 未提供 ms,行为如同提供了 getUTCMilliseconds() 的值。

21.4.4.31 Date.prototype.setUTCMilliseconds ( ms )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. ms 设为 ? ToNumber(ms)。
  5. 如果 tNaN,返回 NaN
  6. timeMakeTime(HourFromTime(t), MinFromTime(t), SecFromTime(t), ms)。
  7. vTimeClip(MakeDate(Day(t), time))。
  8. dateObject.[[DateValue]] 设为 v
  9. 返回 v

21.4.4.32 Date.prototype.setUTCMinutes ( min [ , sec [ , ms ] ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. m 为 ? ToNumber(min)。
  5. 如果 sec 存在,令 s 为 ? ToNumber(sec)。
  6. 如果 ms 存在,令 milli 为 ? ToNumber(ms)。
  7. 如果 tNaN,返回 NaN
  8. 如果 未提供 sec,令 sSecFromTime(t)。
  9. 如果 未提供 ms,令 millimsFromTime(t)。
  10. dateMakeDate(Day(t), MakeTime(HourFromTime(t), m, s, milli))。
  11. vTimeClip(date)。
  12. dateObject.[[DateValue]] 设为 v
  13. 返回 v

此方法的 "length" 属性为 3𝔽

Note

如果 未提供 sec,此方法行为如同提供了 getUTCSeconds() 的值。如果 未提供 ms,行为如同提供了 getUTCMilliseconds() 的返回值。

21.4.4.33 Date.prototype.setUTCMonth ( month [ , date ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. m 为 ? ToNumber(month)。
  5. 如果 date 存在,令 dt 为 ? ToNumber(date)。
  6. 如果 tNaN,返回 NaN
  7. 如果 未提供 date,令 dtDateFromTime(t)。
  8. newDateMakeDate(MakeDay(YearFromTime(t), m, dt), TimeWithinDay(t))。
  9. vTimeClip(newDate)。
  10. dateObject.[[DateValue]] 设为 v
  11. 返回 v

此方法的 "length" 属性为 2𝔽

Note

如果 未提供 date,此方法行为如同提供了 getUTCDate() 的值。

21.4.4.34 Date.prototype.setUTCSeconds ( sec [ , ms ] )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tdateObject.[[DateValue]]
  4. s 为 ? ToNumber(sec)。
  5. 如果 ms 存在,令 milli 为 ? ToNumber(ms)。
  6. 如果 tNaN,返回 NaN
  7. 如果 未提供 ms,令 millimsFromTime(t)。
  8. dateMakeDate(Day(t), MakeTime(HourFromTime(t), MinFromTime(t), s, milli))。
  9. vTimeClip(date)。
  10. dateObject.[[DateValue]] 设为 v
  11. 返回 v

此方法的 "length" 属性为 2𝔽

Note

如果 未提供 ms,此方法行为如同提供了 getUTCMilliseconds() 的值。

21.4.4.35 Date.prototype.toDateString ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tvdateObject.[[DateValue]]
  4. 如果 tvNaN,返回 "Invalid Date"
  5. tLocalTime(tv)。
  6. 返回 DateString(t)。

21.4.4.36 Date.prototype.toISOString ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tvdateObject.[[DateValue]]
  4. 如果 tvNaN,抛出 RangeError 异常。
  5. 断言:tv 为整型 Number。
  6. 如果 tv 对应的年份不能在 日期时间字符串格式 中表示,抛出 RangeError 异常。
  7. 在 UTC 时间尺度上返回包含所有格式元素并附带 UTC 偏移表示 "Z"tv日期时间字符串格式 中的字符串表示。

21.4.4.37 Date.prototype.toJSON ( key )

此方法为 JSON.stringify (25.5.4) 提供 Date 的字符串表示。

调用时执行下列步骤:

  1. obj 为 ? ToObject(this value)。
  2. tv 为 ? ToPrimitive(obj, number)。
  3. 如果 tv 是 Number 且 tv 不是有限值,返回 null
  4. 返回 ? 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 ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tvdateObject.[[DateValue]]
  4. 返回 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:

  1. hourToZeroPaddedDecimalString((HourFromTime(tv)), 2)。
  2. minuteToZeroPaddedDecimalString((MinFromTime(tv)), 2)。
  3. secondToZeroPaddedDecimalString((SecFromTime(tv)), 2)。
  4. 返回 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:

  1. weekday 为表 Table 60 中与 Number WeekDay(tv) 对应项的名称。
  2. month 为表 Table 61 中与 Number MonthFromTime(tv) 对应项的名称。
  3. dayToZeroPaddedDecimalString((DateFromTime(tv)), 2)。
  4. yvYearFromTime(tv)。
  5. 如果 yv+0𝔽yv > +0𝔽,令 yearSign 为空字符串;否则令 yearSign"-"
  6. paddedYearToZeroPaddedDecimalString(abs((yv)), 4)。
  7. 返回 weekday、代码单元 0x0020 (SPACE)、month、代码单元 0x0020 (SPACE)、day、代码单元 0x0020 (SPACE)、yearSignpaddedYear字符串连接
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:

  1. systemTimeZoneIdentifierSystemTimeZoneIdentifier()。
  2. 如果 IsTimeZoneOffsetString(systemTimeZoneIdentifier) 为 true,则
    1. offsetNsParseTimeZoneOffsetString(systemTimeZoneIdentifier)。
  3. 否则,
    1. offsetNsGetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, ((tv) × 106))。
  4. offset𝔽(truncate(offsetNs / 106))。
  5. 如果 offset+0𝔽offset > +0𝔽,则
    1. offsetSign"+"
    2. absOffsetoffset
  6. 否则,
    1. offsetSign"-"
    2. absOffset 为 -offset
  7. offsetMinToZeroPaddedDecimalString((MinFromTime(absOffset)), 2)。
  8. offsetHourToZeroPaddedDecimalString((HourFromTime(absOffset)), 2)。
  9. tzName 为一个实现定义的字符串,该字符串要么为空,要么是代码单元 0x0020 (SPACE)、代码单元 0x0028 (LEFT PARENTHESIS)、实现定义的时区名称和代码单元 0x0029 (RIGHT PARENTHESIS) 的字符串连接
  10. 返回 offsetSignoffsetHouroffsetMintzName字符串连接

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:

  1. 如果 tvNaN,返回 "Invalid Date"
  2. tLocalTime(tv)。
  3. 返回 DateString(t)、代码单元 0x0020 (SPACE)、TimeString(t) 与 TimeZoneString(tv) 的字符串连接

21.4.4.42 Date.prototype.toTimeString ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tvdateObject.[[DateValue]]
  4. 如果 tvNaN,返回 "Invalid Date"
  5. tLocalTime(tv)。
  6. 返回 TimeString(t) 与 TimeZoneString(tv) 的字符串连接

21.4.4.43 Date.prototype.toUTCString ( )

此方法返回表示 this 值所对应时刻的字符串值。该字符串的格式基于 RFC 7231 的 "HTTP-date",并对 ECMAScript Date 支持的完整时间范围进行了推广。

调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. tvdateObject.[[DateValue]]
  4. 如果 tvNaN,返回 "Invalid Date"
  5. weekday 为表 Table 60 中与 Number WeekDay(tv) 对应项的名称。
  6. month 为表 Table 61 中与 Number MonthFromTime(tv) 对应项的名称。
  7. dayToZeroPaddedDecimalString((DateFromTime(tv)), 2)。
  8. yvYearFromTime(tv)。
  9. 如果 yv+0𝔽yv > +0𝔽,令 yearSign 为空字符串;否则令 yearSign"-"
  10. paddedYearToZeroPaddedDecimalString(abs((yv)), 4)。
  11. 返回 weekday","、代码单元 0x0020 (SPACE)、day、代码单元 0x0020 (SPACE)、month、代码单元 0x0020 (SPACE)、yearSignpaddedYear、代码单元 0x0020 (SPACE) 与 TimeString(tv) 的字符串连接

21.4.4.44 Date.prototype.valueOf ( )

此方法在被调用时执行下列步骤:

  1. dateObjectthis 值。
  2. 执行 ? RequireInternalSlot(dateObject, [[DateValue]])。
  3. 返回 dateObject.[[DateValue]]

21.4.4.45 Date.prototype [ %Symbol.toPrimitive% ] ( hint )

此方法由 ECMAScript 语言操作调用以将 Date 转换为原始值。允许的 hint 值为 "default""number""string"。Date 在内置 ECMAScript 对象中是独特的,因为它将 "default" 视为等同于 "string";其它内置对象将 "default" 视为等同于 "number"

调用时执行下列步骤:

  1. objthis 值。
  2. 如果 obj 不是对象,抛出 TypeError 异常。
  3. 如果 hint"string""default",则
    1. tryFirststring
  4. 否则如果 hint"number",则
    1. tryFirstnumber
  5. 否则,
    1. 抛出 TypeError 异常。
  6. 返回 ? OrdinaryToPrimitive(obj, tryFirst)。

此属性具有属性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

此方法的 "name" 属性的值为 "[Symbol.toPrimitive]"

21.4.5 Date 实例的属性

Date 实例是普通对象,从 Date 原型对象继承属性。Date 实例也具有一个 [[DateValue]] 内部槽。[[DateValue]] 内部槽是该 Date 所表示的时间值