21 Numbers and Dates
21.1 Number Objects
21.1.1 The Number Constructor
The Number
- is
%Number% . - is the initial value of the
"Number" property of theglobal object . - creates and initializes a new Number object when called as a
constructor . - performs a type conversion when called as a function rather than as a
constructor . - may be used as the value of an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified Number behaviour must include asupercall to the Numberconstructor to create and initialize the subclass instance with a [[NumberData]] internal slot.
21.1.1.1 Number ( value )
This function performs the following steps when called:
- If value is present, then
- Let primitive be ?
ToNumeric (value). - If primitive
is a BigInt , let n be𝔽 (ℝ (primitive)). - Else, let n be primitive.
- Let primitive be ?
- Else,
- Let n be
+0 𝔽.
- Let n be
- If NewTarget is
undefined , return n. - Let obj be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[NumberData]] »).%Number.prototype% " - Set obj.[[NumberData]] to n.
- Return obj.
21.1.2 Properties of the Number Constructor
The Number
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
21.1.2.1 Number.EPSILON
The value of Number.EPSILON is the
This property has the attributes { [[Writable]]:
21.1.2.2 Number.isFinite ( number )
This function performs the following steps when called:
- If number
is not a Number , returnfalse . - If number is not
finite , returnfalse . - Return
true .
21.1.2.3 Number.isInteger ( number )
This function performs the following steps when called:
- If number is an
integral Number , returntrue . - Return
false .
21.1.2.4 Number.isNaN ( number )
This function performs the following steps when called:
- If number
is not a Number , returnfalse . - If number is
NaN , returntrue . - Return
false .
This function differs from the global isNaN function (
21.1.2.5 Number.isSafeInteger ( number )
An
This function performs the following steps when called:
- If number is an
integral Number , then - Return
false .
21.1.2.6 Number.MAX_SAFE_INTEGER
Due to rounding behaviour necessitated by precision limitations of Number.MAX_SAFE_INTEGER is shared with at least one other 9007199254740992 and 9007199254740993 evaluate to the Number value
The value of Number.MAX_SAFE_INTEGER is
This property has the attributes { [[Writable]]:
21.1.2.7 Number.MAX_VALUE
The value of Number.MAX_VALUE is the largest positive
This property has the attributes { [[Writable]]:
21.1.2.8 Number.MIN_SAFE_INTEGER
Due to rounding behaviour necessitated by precision limitations of Number.MIN_SAFE_INTEGER is shared with at least one other -9007199254740992 and -9007199254740993 evaluate to the Number value
The value of Number.MIN_SAFE_INTEGER is
This property has the attributes { [[Writable]]:
21.1.2.9 Number.MIN_VALUE
The value of Number.MIN_VALUE is the smallest positive value of the
In the Number.MIN_VALUE must be the smallest non-zero positive value that can actually be represented by the implementation.
This property has the attributes { [[Writable]]:
21.1.2.10 Number.NaN
The value of Number.NaN is
This property has the attributes { [[Writable]]:
21.1.2.11 Number.NEGATIVE_INFINITY
The value of Number.NEGATIVE_INFINITY is
This property has the attributes { [[Writable]]:
21.1.2.12 Number.parseFloat ( string )
The initial value of the
21.1.2.13 Number.parseInt ( string, radix )
The initial value of the
21.1.2.14 Number.POSITIVE_INFINITY
The value of Number.POSITIVE_INFINITY is
This property has the attributes { [[Writable]]:
21.1.2.15 Number.prototype
The initial value of Number.prototype is the
This property has the attributes { [[Writable]]:
21.1.3 Properties of the Number Prototype Object
The Number prototype object:
- is
%Number.prototype% . - is an
ordinary object . - is itself a Number object; it has a [[NumberData]] internal slot with the value
+0 𝔽. - has a [[Prototype]] internal slot whose value is
%Object.prototype% .
Unless explicitly stated otherwise, the methods of the Number prototype object defined below are not generic and the
The phrase “this Number value” within the specification of a method refers to the result returned by calling the abstract operation
21.1.3.1 Number.prototype.constructor
The initial value of Number.prototype.constructor is
21.1.3.2 Number.prototype.toExponential ( fractionDigits )
This method returns a String containing this Number value represented in decimal exponential notation with one digit before the significand's decimal point and fractionDigits digits after the significand's decimal point. If fractionDigits is
It performs the following steps when called:
- Let number be ?
ThisNumberValue (this value). - Let fractionCount be ?
ToIntegerOrInfinity (fractionDigits). Assert : If fractionDigits isundefined , then fractionCount is 0.- If number is not
finite , returnNumber::toString (number, 10). - If fractionCount < 0 or fractionCount > 100, throw a
RangeError exception. - Set number to
ℝ (number). - Let sign be the empty String.
- If number < 0, then
- Set sign to
"-" . - Set number to -number.
- Set sign to
- If number = 0, then
- Let significand be the String value consisting of fractionCount + 1 occurrences of the code unit 0x0030 (DIGIT ZERO).
- Let exponent be 0.
- Else,
- If fractionDigits is not
undefined , then- Let exponent and intSignificand be
integers such that 10fractionCount ≤ intSignificand < 10fractionCount + 1 and for which intSignificand × 10exponent - fractionCount - number is as close to zero as possible. If there are two such sets of exponent and intSignificand, pick the exponent and intSignificand for which intSignificand × 10exponent - fractionCount is larger.
- Let exponent and intSignificand be
- Else,
- Let exponent, intSignificand, and ff be
integers such that ff ≥ 0, 10ff ≤ intSignificand < 10ff + 1,𝔽 (intSignificand × 10exponent - ff) is𝔽 (number), and ff is as small as possible. Note that the decimal representation of intSignificand has ff + 1 digits, intSignificand is not divisible by 10, and the least significant digit of intSignificand is not necessarily uniquely determined by these criteria. - Set fractionCount to ff.
- Let exponent, intSignificand, and ff be
- Let significand be the String value consisting of the digits of the decimal representation of intSignificand (in order, with no leading zeroes).
- If fractionDigits is not
- If fractionCount ≠ 0, then
- Let intPart be the first code unit of significand.
- Let fractionalPart be the other fractionCount code units of significand.
- Set significand to the
string-concatenation of intPart,"." , and fractionalPart.
- If exponent = 0, then
- Let exponentSign be
"+" . - Let exponentDigits be
"0" .
- Let exponentSign be
- Else,
- If exponent > 0, then
- Let exponentSign be
"+" .
- Let exponentSign be
- Else,
Assert : exponent < 0.- Let exponentSign be
"-" . - Set exponent to -exponent.
- Let exponentDigits be the String value consisting of the digits of the decimal representation of exponent (in order, with no leading zeroes).
- If exponent > 0, then
- Set significand to the
string-concatenation of significand,"e" , exponentSign, and exponentDigits. - Return the
string-concatenation of sign and significand.
For implementations that provide more accurate conversions than required by the rules above, it is recommended that the following alternative version of step
- Let exponent, intSignificand, and ff be
integers such that ff ≥ 0, 10ff ≤ intSignificand < 10ff + 1,𝔽 (intSignificand × 10exponent - ff) is𝔽 (number), and ff is as small as possible. If there are multiple possibilities for intSignificand, choose intSignificand such that𝔽 (intSignificand × 10exponent - ff) is closest to𝔽 (number). If there are two such possible values of intSignificand, choose the one that is even.
21.1.3.3 Number.prototype.toFixed ( fractionDigits )
This method returns a String containing this Number value represented in decimal fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits is
It performs the following steps when called:
- Let number be ?
ThisNumberValue (this value). - Let fractionCount be ?
ToIntegerOrInfinity (fractionDigits). Assert : If fractionDigits isundefined , then fractionCount is 0.- If fractionCount is not
finite , throw aRangeError exception. - If fractionCount < 0 or fractionCount > 100, throw a
RangeError exception. - If number is not
finite , returnNumber::toString (number, 10). - Set number to
ℝ (number). - Let sign be the empty String.
- If number < 0, then
- Set sign to
"-" . - Set number to -number.
- Set sign to
- If number ≥ 1021, then
- Else,
- Let intValue be an
integer for which intValue / 10fractionCount - number is as close to zero as possible. If there are two such intValue, pick the larger intValue. - If intValue = 0, let digitString be
"0" ; else let digitString be the String value consisting of the digits of the decimal representation of intValue (in order, with no leading zeroes). - If fractionCount ≠ 0, then
- Let digitCount be the length of digitString.
- If digitCount ≤ fractionCount, then
- Let zeroPad be the String value consisting of fractionCount + 1 - digitCount occurrences of the code unit 0x0030 (DIGIT ZERO).
- Set digitString to the
string-concatenation of zeroPad and digitString. - Set digitCount to fractionCount + 1.
- Let intPart be the first digitCount - fractionCount code units of digitString.
- Let fractionalPart be the other fractionCount code units of digitString.
- Set digitString to the
string-concatenation of intPart,"." , and fractionalPart.
- Let intValue be an
- Return the
string-concatenation of sign and digitString.
The output of toFixed may be more precise than toString for some values because toString only prints enough significant digits to distinguish the number from adjacent Number values. For example,
(1000000000000000128).toString() returns
(1000000000000000128).toFixed(0) returns
21.1.3.4 Number.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method produces a String value that represents this Number value formatted according to the conventions of the toString.
The meanings of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
21.1.3.5 Number.prototype.toPrecision ( precision )
This method returns a String containing this Number value represented either in decimal exponential notation with one digit before the significand's decimal point and
It performs the following steps when called:
- Let number be ?
ThisNumberValue (this value). - If precision is
undefined , return !ToString (number). - Let precisionCount be ?
ToIntegerOrInfinity (precision). - If number is not
finite , returnNumber::toString (number, 10). - If precisionCount < 1 or precisionCount > 100, throw a
RangeError exception. - Set number to
ℝ (number). - Let sign be the empty String.
- If number < 0, then
- Set sign to the code unit 0x002D (HYPHEN-MINUS).
- Set number to -number.
- If number = 0, then
- Let significand be the String value consisting of precisionCount occurrences of the code unit 0x0030 (DIGIT ZERO).
- Let exponent be 0.
- Else,
- Let exponent and intSignificand be
integers such that 10precisionCount - 1 ≤ intSignificand < 10precisionCount and for which intSignificand × 10exponent - precisionCount + 1 - number is as close to zero as possible. If there are two such sets of exponent and intSignificand, pick the exponent and intSignificand for which intSignificand × 10exponent - precisionCount + 1 is larger. - Let significand be the String value consisting of the digits of the decimal representation of intSignificand (in order, with no leading zeroes).
- If exponent < -6 or exponent ≥ precisionCount, then
Assert : exponent ≠ 0.- If precisionCount ≠ 1, then
- Let intPart be the first code unit of significand.
- Let fractionalPart be the other precisionCount - 1 code units of significand.
- Set significand to the
string-concatenation of intPart,"." , and fractionalPart.
- If exponent > 0, then
- Let exponentSign be the code unit 0x002B (PLUS SIGN).
- Else,
Assert : exponent < 0.- Let exponentSign be the code unit 0x002D (HYPHEN-MINUS).
- Set exponent to -exponent.
- Let exponentDigits be the String value consisting of the digits of the decimal representation of exponent (in order, with no leading zeroes).
- Return the
string-concatenation of sign, significand, the code unit 0x0065 (LATIN SMALL LETTER E), exponentSign, and exponentDigits.
- Let exponent and intSignificand be
- If exponent = precisionCount - 1, return the
string-concatenation of sign and significand. - If exponent ≥ 0, then
- Set significand to the
string-concatenation of the first exponent + 1 code units of significand, the code unit 0x002E (FULL STOP), and the remaining precisionCount - (exponent + 1) code units of significand.
- Set significand to the
- Else,
- Set significand to the
string-concatenation of the code unit 0x0030 (DIGIT ZERO), the code unit 0x002E (FULL STOP), -(exponent + 1) occurrences of the code unit 0x0030 (DIGIT ZERO), and the String significand.
- Set significand to the
- Return the
string-concatenation of sign and significand.
21.1.3.6 Number.prototype.toString ( [ radix ] )
The optional radix should be an
This method performs the following steps when called:
- Let x be ?
ThisNumberValue (this value). - If radix is
undefined , let radixMV be 10. - Else, let radixMV be ?
ToIntegerOrInfinity (radix). - If radixMV is not in the
inclusive interval from 2 to 36, throw aRangeError exception. - Return
Number::toString (x, radixMV).
This method is not generic; it throws a
The
21.1.3.7 Number.prototype.valueOf ( )
- Return ?
ThisNumberValue (this value).
21.1.3.7.1 ThisNumberValue ( arg )
The abstract operation ThisNumberValue takes argument arg (an
- If arg
is a Number , return arg. - If arg
is an Object and arg has a [[NumberData]] internal slot, then- Let number be arg.[[NumberData]].
Assert : numberis a Number .- Return number.
- Throw a
TypeError exception.
21.1.4 Properties of Number Instances
Number instances are
21.2 BigInt Objects
21.2.1 The BigInt Constructor
The BigInt
- is
%BigInt% . - is the initial value of the
"BigInt" property of theglobal object . - performs a type conversion when called as a function rather than as a
constructor . - is not intended to be used with the
newoperator or to be subclassed. It may be used as the value of anextendsclause of a class definition but asupercall to the BigIntconstructor will cause an exception.
21.2.1.1 BigInt ( value )
This function performs the following steps when called:
- If NewTarget is not
undefined , throw aTypeError exception. - Let primitive be ?
ToPrimitive (value,number ). - If primitive
is a Number , return ?NumberToBigInt (primitive). - Return ?
.ToBigInt (primitive)
21.2.1.1.1 NumberToBigInt ( number )
The abstract operation NumberToBigInt takes argument number (a Number) and returns either a
- If number is not an
integral Number , throw aRangeError exception. - Return
ℤ (ℝ (number)).
21.2.2 Properties of the BigInt Constructor
The BigInt
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
21.2.2.1 BigInt.asIntN ( bits, bigint )
21.2.2.2 BigInt.asUintN ( bits, bigint )
21.2.2.3 BigInt.prototype
The initial value of BigInt.prototype is the
This property has the attributes { [[Writable]]:
21.2.3 Properties of the BigInt Prototype Object
The BigInt prototype object:
- is
%BigInt.prototype% . - is an
ordinary object . is not a BigInt object; it does not have a [[BigIntData]] internal slot.- has a [[Prototype]] internal slot whose value is
%Object.prototype% .
The phrase “this BigInt value” within the specification of a method refers to the result returned by calling the abstract operation
21.2.3.1 BigInt.prototype.constructor
The initial value of BigInt.prototype.constructor is
21.2.3.2 BigInt.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method produces a String value that represents this BigInt value formatted according to the conventions of the toString.
The meanings of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
21.2.3.3 BigInt.prototype.toString ( [ radix ] )
The optional radix should be an
This method performs the following steps when called:
- Let x be ?
ThisBigIntValue (this value). - If radix is
undefined , let radixMV be 10. - Else, let radixMV be ?
ToIntegerOrInfinity (radix). - If radixMV is not in the
inclusive interval from 2 to 36, throw aRangeError exception. - Return
BigInt::toString (x, radixMV).
This method is not generic; it throws a
21.2.3.4 BigInt.prototype.valueOf ( )
- Return ?
ThisBigIntValue (this value).
21.2.3.4.1 ThisBigIntValue ( arg )
The abstract operation ThisBigIntValue takes argument arg (an
- If arg
is a BigInt , return arg. - If arg
is an Object and arg has a [[BigIntData]] internal slot, thenAssert : arg.[[BigIntData]]is a BigInt .- Return arg.[[BigIntData]].
- Throw a
TypeError exception.
21.2.3.5 BigInt.prototype [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
21.2.4 Properties of BigInt Instances
BigInt instances are
21.3 The Math Object
The Math object:
- is
%Math% . - is the initial value of the
"Math" property of theglobal object . - is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is not a
function object . - does not have a [[Construct]] internal method; it cannot be used as a
constructor with thenewoperator. - does not have a [[Call]] internal method; it cannot be invoked as a function.
In this specification, the phrase “the
21.3.1 Value Properties of the Math Object
21.3.1.1 Math.E
The
This property has the attributes { [[Writable]]:
21.3.1.2 Math.LN10
The
This property has the attributes { [[Writable]]:
21.3.1.3 Math.LN2
The
This property has the attributes { [[Writable]]:
21.3.1.4 Math.LOG10E
The
This property has the attributes { [[Writable]]:
The value of Math.LOG10E is approximately the reciprocal of the value of Math.LN10.
21.3.1.5 Math.LOG2E
The
This property has the attributes { [[Writable]]:
The value of Math.LOG2E is approximately the reciprocal of the value of Math.LN2.
21.3.1.6 Math.PI
The
This property has the attributes { [[Writable]]:
21.3.1.7 Math.SQRT1_2
The
This property has the attributes { [[Writable]]:
The value of Math.SQRT1_2 is approximately the reciprocal of the value of Math.SQRT2.
21.3.1.8 Math.SQRT2
The
This property has the attributes { [[Writable]]:
21.3.1.9 Math [ %Symbol.toStringTag% ]
The initial value of the
This property has the attributes { [[Writable]]:
21.3.2 Function Properties of the Math Object
The behaviour of the functions acos, acosh, asin, asinh, atan, atanh, atan2, cbrt, cos, cosh, exp, expm1, hypot, log, log1p, log2, log10, pow, random, sin, sinh, tan, and tanh is not precisely specified here except to require specific results for certain argument values that represent boundary cases of interest. For other argument values, these functions are intended to compute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms. The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform.
Although the choice of algorithms is left to the implementation, it is recommended (but not specified by this standard) that implementations use the approximation algorithms for fdlibm, the freely distributable mathematical library from Sun Microsystems (http://www.netlib.org/fdlibm).
21.3.2.1 Math.abs ( x )
This function returns the absolute value of x; the result has the same magnitude as x but has positive sign.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is
-0 𝔽, return+0 𝔽. - If n is
-∞ 𝔽, return+∞ 𝔽. - If n <
-0 𝔽, return -n. - Return n.
21.3.2.2 Math.acos ( x )
This function returns the inverse cosine of x. The result is expressed in radians and is in the
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , n >1 𝔽, or n <-1 𝔽, returnNaN . - If n is
1 𝔽, return+0 𝔽. - Return an
implementation-approximated Number value representing the inverse cosine ofℝ (n).
21.3.2.3 Math.acosh ( x )
This function returns the inverse hyperbolic cosine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n <
1 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the inverse hyperbolic cosine ofℝ (n).
21.3.2.4 Math.asin ( x )
This function returns the inverse sine of x. The result is expressed in radians and is in the
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n >
1 𝔽 or n <-1 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the inverse sine ofℝ (n).
21.3.2.5 Math.asinh ( x )
This function returns the inverse hyperbolic sine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - Return an
implementation-approximated Number value representing the inverse hyperbolic sine ofℝ (n).
21.3.2.6 Math.atan ( x )
This function returns the inverse tangent of x. The result is expressed in radians and is in the
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is
+∞ 𝔽, return animplementation-approximated Number value representing π / 2. - If n is
-∞ 𝔽, return animplementation-approximated Number value representing -π / 2. - Return an
implementation-approximated Number value representing the inverse tangent ofℝ (n).
21.3.2.7 Math.atanh ( x )
This function returns the inverse hyperbolic tangent of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n >
1 𝔽 or n <-1 𝔽, returnNaN . - If n is
1 𝔽, return+∞ 𝔽. - If n is
-1 𝔽, return-∞ 𝔽. - Return an
implementation-approximated Number value representing the inverse hyperbolic tangent ofℝ (n).
21.3.2.8 Math.atan2 ( y, x )
This function returns the inverse tangent of the quotient
It performs the following steps when called:
- Let ny be ?
ToNumber (y). - Let nx be ?
ToNumber (x). - If ny is
NaN or nx isNaN , returnNaN . - If ny is
+∞ 𝔽, then- If nx is
+∞ 𝔽, return animplementation-approximated Number value representing π / 4. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing 3π / 4. - Return an
implementation-approximated Number value representing π / 2.
- If nx is
- If ny is
-∞ 𝔽, then- If nx is
+∞ 𝔽, return animplementation-approximated Number value representing -π / 4. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing -3π / 4. - Return an
implementation-approximated Number value representing -π / 2.
- If nx is
- If ny is
+0 𝔽, then- If nx >
+0 𝔽 or nx is+0 𝔽, return+0 𝔽. - Return an
implementation-approximated Number value representing π.
- If nx >
- If ny is
-0 𝔽, then- If nx >
+0 𝔽 or nx is+0 𝔽, return-0 𝔽. - Return an
implementation-approximated Number value representing -π.
- If nx >
Assert : ny isfinite and is neither+0 𝔽 nor-0 𝔽.- If ny >
+0 𝔽, then- If nx is
+∞ 𝔽, return+0 𝔽. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing π. - If nx is either
+0 𝔽 or-0 𝔽, return animplementation-approximated Number value representing π / 2.
- If nx is
- If ny <
-0 𝔽, then- If nx is
+∞ 𝔽, return-0 𝔽. - If nx is
-∞ 𝔽, return animplementation-approximated Number value representing -π. - If nx is either
+0 𝔽 or-0 𝔽, return animplementation-approximated Number value representing -π / 2.
- If nx is
Assert : nx isfinite and is neither+0 𝔽 nor-0 𝔽.- Let result be the inverse tangent of
abs (ℝ (ny) /ℝ (nx)). - If nx <
-0 𝔽, then- If ny >
+0 𝔽, set result to π - result. - Else, set result to -π + result.
- If ny >
- Else,
- If ny <
-0 𝔽, set result to -result.
- If ny <
- Return an
implementation-approximated Number value representing result.
21.3.2.9 Math.cbrt ( x )
This function returns the cube root of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - Return an
implementation-approximated Number value representing the cube root ofℝ (n).
21.3.2.10 Math.ceil ( x )
This function returns the smallest (closest to -∞)
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - If n <
-0 𝔽 and n >-1 𝔽, return-0 𝔽. - If n is an
integral Number , return n. - Return the smallest (closest to -∞)
integral Number value that is not less than n.
The value of Math.ceil(x) is the same as the value of -Math.floor(-x).
21.3.2.11 Math.clz32 ( x )
This function performs the following steps when called:
If n is either
21.3.2.12 Math.cos ( x )
This function returns the cosine of x. The argument is expressed in radians.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite , returnNaN . - If n is either
+0 𝔽 or-0 𝔽, return1 𝔽. - Return an
implementation-approximated Number value representing the cosine ofℝ (n).
21.3.2.13 Math.cosh ( x )
This function returns the hyperbolic cosine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is either
+∞ 𝔽 or-∞ 𝔽, return+∞ 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return1 𝔽. - Return an
implementation-approximated Number value representing the hyperbolic cosine ofℝ (n).
The value of Math.cosh(x) is the same as the value of (Math.exp(x) + Math.exp(-x)) / 2.
21.3.2.14 Math.exp ( x )
This function returns the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is either
+0 𝔽 or-0 𝔽, return1 𝔽. - If n is
-∞ 𝔽, return+0 𝔽. - Return an
implementation-approximated Number value representing the exponential function ofℝ (n).
21.3.2.15 Math.expm1 ( x )
This function returns the result of subtracting 1 from the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms). The result is computed in a way that is accurate even when the value of x is close to 0.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽,-0 𝔽, or+∞ 𝔽, return n. - If n is
-∞ 𝔽, return-1 𝔽. - Let exp be the exponential function of
ℝ (n). - Return an
implementation-approximated Number value representing exp - 1.
21.3.2.16 Math.floor ( x )
This function returns the greatest (closest to +∞)
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - If n <
1 𝔽 and n >+0 𝔽, return+0 𝔽. - If n is an
integral Number , return n. - Return the greatest (closest to +∞)
integral Number value that is not greater than n.
The value of Math.floor(x) is the same as the value of -Math.ceil(-x).
21.3.2.17 Math.fround ( x )
This function performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is one of
+0 𝔽,-0 𝔽,+∞ 𝔽, or-∞ 𝔽, return n. - Let n32 be the result of converting n to
IEEE 754-2019 binary32 format using roundTiesToEven mode. - Let n64 be the result of converting n32 to
IEEE 754-2019 binary64 format. - Return the ECMAScript Number value corresponding to n64.
21.3.2.18 Math.f16round ( x )
This function performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is
NaN , returnNaN . - If n is one of
+0 𝔽,-0 𝔽,+∞ 𝔽, or-∞ 𝔽, return n. - Let n16 be the result of converting n to
IEEE 754-2019 binary16 format using roundTiesToEven mode. - Let n64 be the result of converting n16 to
IEEE 754-2019 binary64 format. - Return the ECMAScript Number value corresponding to n64.
This operation is not the same as casting to binary32 and then to binary16 because of the possibility of double-rounding: consider the number k =
Not all platforms provide native support for casting from binary64 to binary16. There are various libraries which can provide this, including the MIT-licensed half library. Alternatively, it is possible to first cast from binary64 to binary32 under roundTiesToEven and then check whether the result could lead to incorrect double-rounding. Such cases can be handled explicitly by adjusting the mantissa of the binary32 value so that it is the value which would be produced by performing the initial cast under roundTiesToOdd. Casting the adjusted value to binary16 under roundTiesToEven then produces the correct value.
21.3.2.19 Math.hypot ( ...args )
Given zero or more arguments, this function returns the square root of the sum of squares of its arguments.
It performs the following steps when called:
- Let coerced be a new empty
List . - For each element arg of args, do
- Let n be ?
ToNumber (arg). - Append n to coerced.
- Let n be ?
- For each element number of coerced, do
- If number is either
+∞ 𝔽 or-∞ 𝔽, return+∞ 𝔽.
- If number is either
- Let onlyZero be
true . - For each element number of coerced, do
- If number is
NaN , returnNaN . - If number is neither
+0 𝔽 nor-0 𝔽, set onlyZero tofalse .
- If number is
- If onlyZero is
true , return+0 𝔽. - Return an
implementation-approximated Number value representing the square root of the sum of squares of themathematical values of the elements of coerced.
The
Implementations should take care to avoid the loss of precision from overflows and underflows that are prone to occur in naive implementations when this function is called with two or more arguments.
21.3.2.20 Math.imul ( x, y )
21.3.2.21 Math.log ( x )
This function returns the natural logarithm of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return-∞ 𝔽. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representingln (ℝ (n)).
21.3.2.22 Math.log1p ( x )
This function returns the natural logarithm of 1 + x. The result is computed in a way that is accurate even when the value of x is close to zero.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽,-0 𝔽, or+∞ 𝔽, return n. - If n is
-1 𝔽, return-∞ 𝔽. - If n <
-1 𝔽, returnNaN . - Return an
implementation-approximated Number value representingln (1 +ℝ (n)).
21.3.2.23 Math.log10 ( x )
This function returns the base 10 logarithm of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return-∞ 𝔽. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representinglog10 (ℝ (n)).
21.3.2.24 Math.log2 ( x )
This function returns the base 2 logarithm of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is either
NaN or+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is either
+0 𝔽 or-0 𝔽, return-∞ 𝔽. - If n <
-0 𝔽, returnNaN . - Return an
implementation-approximated Number value representinglog2 (ℝ (n)).
21.3.2.25 Math.max ( ...args )
Given zero or more arguments, this function calls
It performs the following steps when called:
- Let coerced be a new empty
List . - For each element arg of args, do
- Let n be ?
ToNumber (arg). - Append n to coerced.
- Let n be ?
- Let highest be
-∞ 𝔽. - For each element number of coerced, do
- If number is
NaN , returnNaN . - If number is
+0 𝔽 and highest is-0 𝔽, set highest to+0 𝔽. - If number > highest, set highest to number.
- If number is
- Return highest.
The comparison of values to determine the largest value is done using the
The
21.3.2.26 Math.min ( ...args )
Given zero or more arguments, this function calls
It performs the following steps when called:
- Let coerced be a new empty
List . - For each element arg of args, do
- Let n be ?
ToNumber (arg). - Append n to coerced.
- Let n be ?
- Let lowest be
+∞ 𝔽. - For each element number of coerced, do
- If number is
NaN , returnNaN . - If number is
-0 𝔽 and lowest is+0 𝔽, set lowest to-0 𝔽. - If number < lowest, set lowest to number.
- If number is
- Return lowest.
The comparison of values to determine the smallest value is done using the
The
21.3.2.27 Math.pow ( base, exponent )
This function performs the following steps when called:
- Set base to ?
ToNumber (base). - Set exponent to ?
ToNumber (exponent). - Return
Number::exponentiate (base, exponent).
21.3.2.28 Math.random ( )
This function returns a Number value with positive sign, greater than or equal to
Each Math.random function created for distinct
21.3.2.29 Math.round ( x )
This function returns the Number value that is closest to x and is integral. If two
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is anintegral Number , return n. - If n <
0.5 𝔽 and n >+0 𝔽, return+0 𝔽. - If n <
-0 𝔽 and n ≥-0.5 𝔽, return-0 𝔽. - Return the
integral Number closest to n, preferring the Number closer to +∞ in the case of a tie.
Math.round(3.5) returns 4, but Math.round(-3.5) returns -3.
The value of Math.round(x) is not always the same as the value of Math.floor(x + 0.5). When x is x is less than Math.round(x) returns Math.floor(x + 0.5) returns Math.round(x) may also differ from the value of Math.floor(x + 0.5)because of internal rounding when computing x + 0.5.
21.3.2.30 Math.sign ( x )
This function returns the sign of x, indicating whether x is positive, negative, or zero.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n <
-0 𝔽, return-1 𝔽. - Return
1 𝔽.
21.3.2.31 Math.sin ( x )
This function returns the sine of x. The argument is expressed in radians.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is either
+∞ 𝔽 or-∞ 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the sine ofℝ (n).
21.3.2.32 Math.sinh ( x )
This function returns the hyperbolic sine of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - Return an
implementation-approximated Number value representing the hyperbolic sine ofℝ (n).
The value of Math.sinh(x) is the same as the value of (Math.exp(x) - Math.exp(-x)) / 2.
21.3.2.33 Math.sqrt ( x )
21.3.2.34 Math.sumPrecise ( items )
Given an
It performs the following steps when called:
- Perform ?
RequireObjectCoercible (items). - Let iteratorRecord be ?
GetIterator (items,sync ). - Let state be
minus-zero . - Let sum be 0.
- Let count be 0.
- Let next be
not-started . - Repeat, while next is not
done ,- Set next to ?
IteratorStepValue (iteratorRecord). - If next is not
done , then- If count ≥ 253 - 1, then
NOTE : This step is not expected to be reached in practice and is included only so that implementations may rely on inputs being “reasonably sized” without violating this specification.- Let error be
ThrowCompletion (a newly createdRangeError object). - Return ?
IteratorClose (iteratorRecord, error).
- If next
is not a Number , then- Let error be
ThrowCompletion (a newly createdTypeError object). - Return ?
IteratorClose (iteratorRecord, error).
- Let error be
- Let n be next.
- If state is not
not-a-number , then- If n is
NaN , then- Set state to
not-a-number .
- Set state to
- Else if n is
+∞ 𝔽, then- If state is
minus-infinity , set state tonot-a-number . - Else, set state to
plus-infinity .
- If state is
- Else if n is
-∞ 𝔽, then- If state is
plus-infinity , set state tonot-a-number . - Else, set state to
minus-infinity .
- If state is
- Else if n is not
-0 𝔽 and state is eitherminus-zero orfinite , then- Set state to
finite . - Set sum to sum +
ℝ (n).
- Set state to
- If n is
- Set count to count + 1.
- If count ≥ 253 - 1, then
- Set next to ?
- If state is
not-a-number , returnNaN . - If state is
plus-infinity , return+∞ 𝔽. - If state is
minus-infinity , return-∞ 𝔽. - If state is
minus-zero , return-0 𝔽. - Return
𝔽 (sum).
The value of sum can be computed without arbitrary-precision arithmetic by a variety of algorithms. One such is the “Grow-Expansion” algorithm given in Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates by Jonathan Richard Shewchuk. A more recent algorithm is given in “Fast exact summation using small and large superaccumulators”, code for which is available at https://gitlab.com/radfordneal/xsum.
21.3.2.35 Math.tan ( x )
This function returns the tangent of x. The argument is expressed in radians.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is either
+∞ 𝔽 or-∞ 𝔽, returnNaN . - Return an
implementation-approximated Number value representing the tangent ofℝ (n).
21.3.2.36 Math.tanh ( x )
This function returns the hyperbolic tangent of x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is one of
NaN ,+0 𝔽, or-0 𝔽, return n. - If n is
+∞ 𝔽, return1 𝔽. - If n is
-∞ 𝔽, return-1 𝔽. - Return an
implementation-approximated Number value representing the hyperbolic tangent ofℝ (n).
The value of Math.tanh(x) is the same as the value of (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x)).
21.3.2.37 Math.trunc ( x )
This function returns the integral part of the number x, removing any fractional digits. If x is already integral, the result is x.
It performs the following steps when called:
- Let n be ?
ToNumber (x). - If n is not
finite or n is either+0 𝔽 or-0 𝔽, return n. - If n <
1 𝔽 and n >+0 𝔽, return+0 𝔽. - If n <
-0 𝔽 and n >-1 𝔽, return-0 𝔽. - Return the
integral Number nearest n in the direction of+0 𝔽.
21.4 Date Objects
21.4.1 Overview of Date Objects and Definitions of Abstract Operations
The following
21.4.1.1 Time Values and Time Range
Time measurement in ECMAScript is analogous to time measurement in POSIX, in particular sharing definition in terms of the proleptic Gregorian calendar, an epoch of midnight at the beginning of 1 January 1970 UTC, and an accounting of every day as comprising exactly 86,400 seconds (each of which is 1000 milliseconds long).
An ECMAScript time value
Time values do not account for UTC leap seconds—there are no time values representing instants within positive leap seconds, and there are time values representing instants removed from the UTC timeline by negative leap seconds. However, the definition of time values nonetheless yields piecewise alignment with UTC, with discontinuities only at leap second boundaries and zero difference outside of leap seconds.
A Number can exactly represent all
The exact moment of midnight at the beginning of 1 January 1970 UTC is represented by the time value
In the proleptic Gregorian calendar, leap years are precisely those which are both divisible by 4 and either divisible by 400 or not divisible by 100.
The 400 year cycle of the proleptic Gregorian calendar contains 97 leap years. This yields an average of 365.2425 days per year, which is 31,556,952,000 milliseconds. Therefore, the maximum range a Number could represent exactly with millisecond precision is approximately -285,426 to 285,426 years relative to 1970. The smaller range supported by a time value as specified in this section is approximately -273,790 to 273,790 years relative to 1970.
21.4.1.2 Time-related Constants
These constants are referenced by algorithms in the following sections.
21.4.1.3 Day ( tv )
The abstract operation Day takes argument tv (a
21.4.1.4 TimeWithinDay ( tv )
The abstract operation TimeWithinDay takes argument tv (a
21.4.1.5 DaysInYear ( y )
The abstract operation DaysInYear takes argument y (an
21.4.1.6 DayFromYear ( y )
The abstract operation DayFromYear takes argument y (an
- Let ry be
ℝ (y). NOTE : In the following steps, numberYears1, numberYears4, numberYears100, and numberYears400 represent the number of years divisible by 1, 4, 100, and 400, respectively, that occur between theepoch and the start of year y. The number is negative if y is before theepoch .- Let numberYears1 be (ry - 1970).
- Let numberYears4 be
floor ((ry - 1969) / 4). - Let numberYears100 be
floor ((ry - 1901) / 100). - Let numberYears400 be
floor ((ry - 1601) / 400). - Return
𝔽 (365 × numberYears1 + numberYears4 - numberYears100 + numberYears400).
21.4.1.7 TimeFromYear ( y )
The abstract operation TimeFromYear takes argument y (an
- Return
msPerDay ×DayFromYear (y).
21.4.1.8 YearFromTime ( tv )
The abstract operation YearFromTime takes argument tv (a
- Return the largest
integral Number y (closest to +∞) such thatTimeFromYear (y) ≤ tv.
21.4.1.9 DayWithinYear ( tv )
The abstract operation DayWithinYear takes argument tv (a
- Return
Day (tv) -DayFromYear (YearFromTime (tv)).
21.4.1.10 InLeapYear ( tv )
The abstract operation InLeapYear takes argument tv (a
- If
DaysInYear (YearFromTime (tv)) is366 𝔽, return1 𝔽. - Return
+0 𝔽.
21.4.1.11 MonthFromTime ( tv )
The abstract operation MonthFromTime takes argument tv (a
- Let inLeapYear be
InLeapYear (tv). - Let dayWithinYear be
DayWithinYear (tv). - If dayWithinYear <
31 𝔽, return+0 𝔽. - If dayWithinYear <
59 𝔽 + inLeapYear, return1 𝔽. - If dayWithinYear <
90 𝔽 + inLeapYear, return2 𝔽. - If dayWithinYear <
120 𝔽 + inLeapYear, return3 𝔽. - If dayWithinYear <
151 𝔽 + inLeapYear, return4 𝔽. - If dayWithinYear <
181 𝔽 + inLeapYear, return5 𝔽. - If dayWithinYear <
212 𝔽 + inLeapYear, return6 𝔽. - If dayWithinYear <
243 𝔽 + inLeapYear, return7 𝔽. - If dayWithinYear <
273 𝔽 + inLeapYear, return8 𝔽. - If dayWithinYear <
304 𝔽 + inLeapYear, return9 𝔽. - If dayWithinYear <
334 𝔽 + inLeapYear, return10 𝔽. Assert : dayWithinYear <365 𝔽 + inLeapYear.- Return
11 𝔽.
21.4.1.12 DateFromTime ( tv )
The abstract operation DateFromTime takes argument tv (a
- Let inLeapYear be
InLeapYear (tv). - Let dayWithinYear be
DayWithinYear (tv). - Let month be
MonthFromTime (tv). - If month is
+0 𝔽, return dayWithinYear +1 𝔽. - If month is
1 𝔽, return dayWithinYear -30 𝔽. - If month is
2 𝔽, return dayWithinYear -58 𝔽 - inLeapYear. - If month is
3 𝔽, return dayWithinYear -89 𝔽 - inLeapYear. - If month is
4 𝔽, return dayWithinYear -119 𝔽 - inLeapYear. - If month is
5 𝔽, return dayWithinYear -150 𝔽 - inLeapYear. - If month is
6 𝔽, return dayWithinYear -180 𝔽 - inLeapYear. - If month is
7 𝔽, return dayWithinYear -211 𝔽 - inLeapYear. - If month is
8 𝔽, return dayWithinYear -242 𝔽 - inLeapYear. - If month is
9 𝔽, return dayWithinYear -272 𝔽 - inLeapYear. - If month is
10 𝔽, return dayWithinYear -303 𝔽 - inLeapYear. Assert : month is11 𝔽.- Return dayWithinYear -
333 𝔽 - inLeapYear.
21.4.1.13 WeekDay ( tv )
The abstract operation WeekDay takes argument tv (a
21.4.1.14 HourFromTime ( tv )
The abstract operation HourFromTime takes argument tv (a
21.4.1.15 MinFromTime ( tv )
The abstract operation MinFromTime takes argument tv (a
- Return
𝔽 (floor (ℝ (tv /msPerMinute ))modulo MinutesPerHour ).
21.4.1.16 SecFromTime ( tv )
The abstract operation SecFromTime takes argument tv (a
- Return
𝔽 (floor (ℝ (tv /msPerSecond ))modulo SecondsPerMinute ).
21.4.1.17 msFromTime ( tv )
The abstract operation msFromTime takes argument tv (a
- Return
𝔽 (ℝ (tv)modulo ℝ (msPerSecond )).
21.4.1.18 GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond, microsecond, nanosecond )
The abstract operation GetUTCEpochNanoseconds takes arguments year (an
21.4.1.19 Time Zone Identifiers
Time zones in ECMAScript are represented by time zone identifiers, which are Strings composed entirely of code units in the
A primary time zone identifier is the preferred identifier for an available named time zone. A non-primary time zone identifier is an identifier for an available named time zone that is not a primary time zone identifier. An available named time zone identifier is either a primary time zone identifier or a non-primary time zone identifier. Each available named time zone identifier is associated with exactly one available named time zone. Each available named time zone is associated with exactly one primary time zone identifier and zero or more non-primary time zone identifiers.
ECMAScript implementations must support an available named time zone with the identifier
Implementations that follow the requirements for time zones as described in the ECMA-402 Internationalization API specification are called time zone aware.
Time zone aware implementations must support available named time zones corresponding to the Zone and Link names of the IANA Time Zone Database, and only such names.
In time zone aware implementations, a primary time zone identifier is a Zone name, and a non-primary time zone identifier is a Link name, respectively, in the IANA Time Zone Database except as specifically overridden by
21.4.1.20 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond )
The
When the input represents a local time occurring more than once because of a negative time zone transition (e.g. when daylight saving time ends or the time zone offset is decreased due to a time zone rule change), the returned
The default implementation of GetNamedTimeZoneEpochNanoseconds, to be used for ECMAScript implementations that do not include local political rules for any time zones, performs the following steps when called:
Assert : timeZoneIdentifier is"UTC" .- Let epochNanoseconds be
GetUTCEpochNanoseconds (year, month, day, hour, minute, second, millisecond, microsecond, nanosecond). - Return « epochNanoseconds ».
It is required for
1:30 AM on 5 November 2017 in America/New_York is repeated twice, so GetNamedTimeZoneEpochNanoseconds(
2:30 AM on 12 March 2017 in America/New_York does not exist, so GetNamedTimeZoneEpochNanoseconds(
21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds )
The
The returned
The default implementation of GetNamedTimeZoneOffsetNanoseconds, to be used for ECMAScript implementations that do not include local political rules for any time zones, performs the following steps when called:
Assert : timeZoneIdentifier is"UTC" .- Return 0.
Time zone offset values may be positive or negative.
21.4.1.22 Time Zone Identifier Record
A Time Zone Identifier Record is a
Time Zone Identifier Records have the fields listed in
| Field Name | Value | Meaning |
|---|---|---|
| [[Identifier]] | a String | An |
| [[PrimaryIdentifier]] | a String | The |
If [[Identifier]] is a
21.4.1.23 AvailableNamedTimeZoneIdentifiers ( )
The
- If the implementation does not include local political rules for any time zones, then
- Return « the
Time Zone Identifier Record { [[Identifier]]:"UTC" , [[PrimaryIdentifier]]:"UTC" } ».
- Return « the
- Let identifiers be the
List of uniqueavailable named time zone identifiers , sorted according tolexicographic code unit order . - Let result be a new empty
List . - For each element identifier of identifiers, do
- Let primary be identifier.
- If identifier is a
non-primary time zone identifier in this implementation and identifier is not"UTC" , then- Set primary to the
primary time zone identifier associated with identifier. NOTE : An implementation may need to resolve identifier iteratively to obtain theprimary time zone identifier .
- Set primary to the
- Let record be the
Time Zone Identifier Record { [[Identifier]]: identifier, [[PrimaryIdentifier]]: primary }. - Append record to result.
Assert : result contains aTime Zone Identifier Record record such that record.[[Identifier]] is"UTC" and record.[[PrimaryIdentifier]] is"UTC" .- Return result.
21.4.1.24 SystemTimeZoneIdentifier ( )
The
- If the implementation only supports the UTC time zone, return
"UTC" . - Let systemTimeZoneString be the String representing the
host environment 's current time zone, either aprimary time zone identifier or anoffset time zone identifier. - Return systemTimeZoneString.
To ensure the level of functionality that implementations commonly provide in the methods of the Date object, it is recommended that SystemTimeZoneIdentifier return an IANA time zone name corresponding to the
For example, if the
21.4.1.25 LocalTime ( tv )
The abstract operation LocalTime takes argument tv (a
- Let systemTimeZoneIdentifier be
SystemTimeZoneIdentifier (). - If
IsTimeZoneOffsetString (systemTimeZoneIdentifier) istrue , then- Let offsetNs be
ParseTimeZoneOffsetString (systemTimeZoneIdentifier).
- Let offsetNs be
- Else,
- Let offsetNs be
GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier,ℤ (ℝ (tv) × 106)).
- Let offsetNs be
- Let offsetMs be
truncate (offsetNs / 106). - Return tv +
𝔽 (offsetMs).
If political rules for the local time tv are not available within the implementation, the result is tv because
It is required for
Two different input
21.4.1.26 UTC ( t )
The abstract operation UTC takes argument t (a Number) and returns a
- If t is not
finite , returnNaN . - Let systemTimeZoneIdentifier be
SystemTimeZoneIdentifier (). - If
IsTimeZoneOffsetString (systemTimeZoneIdentifier) istrue , then- Let offsetNs be
ParseTimeZoneOffsetString (systemTimeZoneIdentifier).
- Let offsetNs be
- Else,
- Let possibleInstants be
GetNamedTimeZoneEpochNanoseconds (systemTimeZoneIdentifier,ℝ (YearFromTime (t)),ℝ (MonthFromTime (t)) + 1,ℝ (DateFromTime (t)),ℝ (HourFromTime (t)),ℝ (MinFromTime (t)),ℝ (SecFromTime (t)),ℝ (msFromTime (t)), 0, 0). NOTE : The following steps ensure that when t represents local time repeating multiple times at a negative time zone transition (e.g. when the daylight saving time ends or the time zone offset is decreased due to a time zone rule change) or skipped local time at a positive time zone transition (e.g. when the daylight saving time starts or the time zone offset is increased due to a time zone rule change), t is interpreted using the time zone offset before the transition.- If possibleInstants is not empty, then
- Let disambiguatedInstant be possibleInstants[0].
- Else,
NOTE : t represents a local time skipped at a positive time zone transition (e.g. due to daylight saving time starting or a time zone rule change increasing the UTC offset).- Let possibleInstantsBefore be
GetNamedTimeZoneEpochNanoseconds (systemTimeZoneIdentifier,ℝ (YearFromTime (tBefore)),ℝ (MonthFromTime (tBefore)) + 1,ℝ (DateFromTime (tBefore)),ℝ (HourFromTime (tBefore)),ℝ (MinFromTime (tBefore)),ℝ (SecFromTime (tBefore)),ℝ (msFromTime (tBefore)), 0, 0), where tBefore is the largestintegral Number < t for which possibleInstantsBefore is not empty (i.e., tBefore represents the last local time before the transition). - Let disambiguatedInstant be the last element of possibleInstantsBefore.
- Let offsetNs be
GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier, disambiguatedInstant).
- Let possibleInstants be
- Let offsetMs be
truncate (offsetNs / 106). - Return t -
𝔽 (offsetMs).
Input t is nominally a
If political rules for the local time t are not available within the implementation, the result is t because
It is required for
1:30 AM on 5 November 2017 in America/New_York is repeated twice (fall backward), but it must be interpreted as 1:30 AM UTC-04 instead of 1:30 AM UTC-05.
In UTC(
2:30 AM on 12 March 2017 in America/New_York does not exist, but it must be interpreted as 2:30 AM UTC-05 (equivalent to 3:30 AM UTC-04).
In UTC(
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 calculates a number of milliseconds. It performs the following steps when called:
- If hour is not
finite , min is notfinite , sec is notfinite , or ms is notfinite , returnNaN . - Let h be
𝔽 (!ToIntegerOrInfinity (hour)). - Let m be
𝔽 (!ToIntegerOrInfinity (min)). - Let s be
𝔽 (!ToIntegerOrInfinity (sec)). - Let milli be
𝔽 (!ToIntegerOrInfinity (ms)). - Return ((h ×
msPerHour + m ×msPerMinute ) + s ×msPerSecond ) + milli.
The arithmetic in MakeTime is floating-point arithmetic, which is not associative, so the operations must be performed in the correct order.
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
- If year is not
finite , month is notfinite , or date is notfinite , returnNaN . - Let y be
𝔽 (!ToIntegerOrInfinity (year)). - Let m be
𝔽 (!ToIntegerOrInfinity (month)). - Let dt be
𝔽 (!ToIntegerOrInfinity (date)). - Let ym be y +
𝔽 (floor (ℝ (m) / 12)). - If ym is not
finite , returnNaN . - Let mn be
𝔽 (ℝ (m)modulo 12). - Find a
finite time value tv such thatYearFromTime (tv) is ym,MonthFromTime (tv) is mn, andDateFromTime (tv) is1 𝔽; but if this is not possible (because some argument is out of range), returnNaN . - Return
Day (tv) + dt -1 𝔽.
21.4.1.29 MakeDate ( day, time )
The abstract operation MakeDate takes arguments day (a Number) and time (a Number) and returns a
21.4.1.30 MakeFullYear ( year )
The abstract operation MakeFullYear takes argument year (a Number) and returns an
- If year is one of
NaN ,+∞ 𝔽, or-∞ 𝔽, returnNaN . - Let truncated be !
ToIntegerOrInfinity (year). - If truncated is in the
inclusive interval from 0 to 99, return1900 𝔽 +𝔽 (truncated). - Return
𝔽 (truncated).
21.4.1.31 TimeClip ( time )
The abstract operation TimeClip takes argument time (a Number) and returns a
- If time is not
finite , returnNaN . - If
abs (ℝ (time)) > 8.64 × 1015, returnNaN . - Return
𝔽 (!ToIntegerOrInfinity (time)).
21.4.1.32 Date Time String Format
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Where the elements are as follows:
YYYY
|
is the year in the proleptic Gregorian calendar as four decimal digits from 0000 to 9999, or as an |
-
|
|
MM
|
is the month of the year as two decimal digits from 01 (January) to 12 (December). |
DD
|
is the day of the month as two decimal digits from 01 to 31. |
T
|
|
HH
|
is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24. |
:
|
|
mm
|
is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59. |
ss
|
is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59. |
.
|
|
sss
|
is the number of complete milliseconds since the start of the second as three decimal digits. |
Z
|
is the UTC offset representation specified as HH:mm (a subset of the |
This format includes date-only forms:
YYYY
YYYY-MM
YYYY-MM-DD
It also includes “date-time” forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional UTC offset representation appended:
THH:mm
THH:mm:ss
THH:mm:ss.sss
A string containing out-of-bounds or nonconforming elements is not a valid instance of this format.
As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time: 1995-02-04T24:00 and 1995-02-05T00:00. This interpretation of the latter form as “end of a calendar day” is consistent with ISO 8601, even though that specification reserves it for describing time intervals and does not permit it within representations of single points in time.
There exists no international standard that specifies abbreviations for civil time zones like CET, EST, etc. and sometimes the same abbreviation is even used for two very different time zones. For this reason, both ISO 8601 and this format specify numeric representations of time zone offsets.
21.4.1.32.1 Expanded Years
Covering the full Date.parse
Examples of date-
| -271821-04-20T00:00:00Z | 271822 B.C. |
| -000001-01-01T00:00:00Z | 2 B.C. |
| +000000-01-01T00:00:00Z | 1 B.C. |
| +000001-01-01T00:00:00Z | 1 A.D. |
| +001970-01-01T00:00:00Z | 1970 A.D. |
| +002009-12-15T00:00:00Z | 2009 A.D. |
| +275760-09-13T00:00:00Z | 275760 A.D. |
21.4.1.33 Time Zone Offset String Format
ECMAScript defines a string interchange format for UTC offsets, derived from ISO 8601. The format is described by the following grammar.
Syntax
21.4.1.33.1 IsTimeZoneOffsetString ( offsetString )
The abstract operation IsTimeZoneOffsetString takes argument offsetString (a String) and returns a Boolean. The return value indicates whether offsetString conforms to the grammar given by
21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString )
The abstract operation ParseTimeZoneOffsetString takes argument offsetString (a String) and returns an
- Let parseResult be
ParseText (offsetString,UTCOffset ). Assert : parseResult is not aList of errors.Assert : parseResult contains aASCIISign Parse Node .- Let parsedSign be the
source text matched by theASCIISign Parse Node contained within parseResult. - If parsedSign is the single code point U+002D (HYPHEN-MINUS), then
- Let sign be -1.
- Else,
- Let sign be 1.
NOTE : Applications ofStringToNumber below do not lose precision, since each of the parsed values is guaranteed to be a sufficiently short string of decimal digits.Assert : parseResult contains anHour Parse Node .- Let parsedHours be the
source text matched by theHour Parse Node contained within parseResult. - Let hours be
ℝ (StringToNumber (CodePointsToString (parsedHours))). - If parseResult does not contain a
MinuteSecond Parse Node , then- Let minutes be 0.
- Else,
- Let parsedMinutes be the
source text matched by the firstMinuteSecond Parse Node contained within parseResult. - Let minutes be
ℝ (StringToNumber (CodePointsToString (parsedMinutes))).
- Let parsedMinutes be the
- If parseResult does not contain two
MinuteSecond Parse Nodes , then- Let seconds be 0.
- Else,
- Let parsedSeconds be the
source text matched by the secondMinuteSecond Parse Node contained within parseResult. - Let seconds be
ℝ (StringToNumber (CodePointsToString (parsedSeconds))).
- Let parsedSeconds be the
- If parseResult does not contain a
TemporalDecimalFraction Parse Node , then- Let nanoseconds be 0.
- Else,
- Let parsedFraction be the
source text matched by theTemporalDecimalFraction Parse Node contained within parseResult. - Let fraction be the
string-concatenation ofCodePointsToString (parsedFraction) and"000000000" . - Let nanosecondsString be the
substring of fraction from 1 to 10. - Let nanoseconds be
ℝ (StringToNumber (nanosecondsString)).
- Let parsedFraction be the
- Return sign × (((hours × 60 + minutes) × 60 + seconds) × 109 + nanoseconds).
21.4.2 The Date Constructor
The Date
- is
%Date% . - is the initial value of the
"Date" property of theglobal object . - creates and initializes a new Date when called as a
constructor . - returns a String representing the current time (UTC) when called as a function rather than as a
constructor . - is a function whose behaviour differs based upon the number and types of its arguments.
- may be used as the value of an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified Date behaviour must include asupercall to the Dateconstructor to create and initialize the subclass instance with a [[DateValue]] internal slot.
21.4.2.1 Date ( ...values )
This function performs the following steps when called:
- If NewTarget is
undefined , then- Let now be the
time value (UTC) identifying the current time. - Return
ToDateString (now).
- Let now be the
- Let numberOfArgs be the number of elements in values.
- If numberOfArgs = 0, then
- Let dv be the
time value (UTC) identifying the current time.
- Let dv be the
- Else if numberOfArgs = 1, then
- Let value be values[0].
- If value
is an Object and value has a [[DateValue]] internal slot, then- Let tv be value.[[DateValue]].
- Else,
- Let v be ?
ToPrimitive (value). - If v
is a String , thenAssert : The next step never returns anabrupt completion because vis a String .- Let tv be the result of parsing v as a date, in exactly the same manner as for the
parsemethod (21.4.3.2 ).
- Else,
- Let tv be ?
ToNumber (v).
- Let tv be ?
- Let v be ?
- Let dv be
TimeClip (tv).
- Else,
Assert : numberOfArgs ≥ 2.- Let y be ?
ToNumber (values[0]). - Let m be ?
ToNumber (values[1]). - If numberOfArgs > 2, let dt be ?
ToNumber (values[2]); else let dt be1 𝔽. - If numberOfArgs > 3, let h be ?
ToNumber (values[3]); else let h be+0 𝔽. - If numberOfArgs > 4, let min be ?
ToNumber (values[4]); else let min be+0 𝔽. - If numberOfArgs > 5, let s be ?
ToNumber (values[5]); else let s be+0 𝔽. - If numberOfArgs > 6, let milli be ?
ToNumber (values[6]); else let milli be+0 𝔽. - Let yr be
MakeFullYear (y). - Let finalDate be
MakeDate (MakeDay (yr, m, dt),MakeTime (h, min, s, milli)). - Let dv be
TimeClip (UTC (finalDate)).
- Let obj be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[DateValue]] »).%Date.prototype% " - Set obj.[[DateValue]] to dv.
- Return obj.
21.4.3 Properties of the Date Constructor
The Date
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has a
"length" property whose value is7 𝔽. - has the following properties:
21.4.3.1 Date.now ( )
This function returns the
21.4.3.2 Date.parse ( string )
This function applies the
If the String conforms to the MM or DD elements are absent, HH, mm, or ss elements are absent, sss element is absent,
If x is any Date whose milliseconds amount is zero within a particular implementation of ECMAScript, then all of the following expressions should produce the same numeric value in that implementation, if all the properties referenced have their initial values:
x.valueOf()
Date.parse(x.toString())
Date.parse(x.toUTCString())
Date.parse(x.toISOString())
However, the expression
Date.parse(x.toLocaleString())
is not required to produce the same Number value as the preceding three expressions and, in general, the value produced by this function is toString or toUTCString method.
21.4.3.3 Date.prototype
The initial value of Date.prototype is the
This property has the attributes { [[Writable]]:
21.4.3.4 Date.UTC ( year [ , month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ] )
This function performs the following steps when called:
- Let y be ?
ToNumber (year). - If month is present, let m be ?
ToNumber (month); else let m be+0 𝔽. - If date is present, let dt be ?
ToNumber (date); else let dt be1 𝔽. - If hours is present, let h be ?
ToNumber (hours); else let h be+0 𝔽. - If minutes is present, let min be ?
ToNumber (minutes); else let min be+0 𝔽. - If seconds is present, let s be ?
ToNumber (seconds); else let s be+0 𝔽. - If ms is present, let milli be ?
ToNumber (ms); else let milli be+0 𝔽. - Let yr be
MakeFullYear (y). - Return
TimeClip (MakeDate (MakeDay (yr, m, dt),MakeTime (h, min, s, milli))).
The
This function differs from the Date
21.4.4 Properties of the Date Prototype Object
The Date prototype object:
- is
%Date.prototype% . - is itself an
ordinary object . - is not a Date instance and does not have a [[DateValue]] internal slot.
- has a [[Prototype]] internal slot whose value is
%Object.prototype% .
Unless explicitly defined otherwise, the methods of the Date prototype object defined below are not generic and the
21.4.4.1 Date.prototype.constructor
The initial value of Date.prototype.constructor is
21.4.4.2 Date.prototype.getDate ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
DateFromTime (LocalTime (tv)).
21.4.4.3 Date.prototype.getDay ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
WeekDay (LocalTime (tv)).
21.4.4.4 Date.prototype.getFullYear ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
YearFromTime (LocalTime (tv)).
21.4.4.5 Date.prototype.getHours ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
HourFromTime (LocalTime (tv)).
21.4.4.6 Date.prototype.getMilliseconds ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
msFromTime (LocalTime (tv)).
21.4.4.7 Date.prototype.getMinutes ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
MinFromTime (LocalTime (tv)).
21.4.4.8 Date.prototype.getMonth ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
MonthFromTime (LocalTime (tv)).
21.4.4.9 Date.prototype.getSeconds ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
SecFromTime (LocalTime (tv)).
21.4.4.10 Date.prototype.getTime ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Return dateObj.[[DateValue]].
21.4.4.11 Date.prototype.getTimezoneOffset ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return (tv -
LocalTime (tv)) /msPerMinute .
21.4.4.12 Date.prototype.getUTCDate ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
DateFromTime (tv).
21.4.4.13 Date.prototype.getUTCDay ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
WeekDay (tv).
21.4.4.14 Date.prototype.getUTCFullYear ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
YearFromTime (tv).
21.4.4.15 Date.prototype.getUTCHours ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
HourFromTime (tv).
21.4.4.16 Date.prototype.getUTCMilliseconds ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
msFromTime (tv).
21.4.4.17 Date.prototype.getUTCMinutes ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
MinFromTime (tv).
21.4.4.18 Date.prototype.getUTCMonth ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
MonthFromTime (tv).
21.4.4.19 Date.prototype.getUTCSeconds ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , returnNaN . - Return
SecFromTime (tv).
21.4.4.20 Date.prototype.setDate ( date )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let dt be ?
ToNumber (date). - If tv is
NaN , returnNaN . - Set tv to
LocalTime (tv). - Let newDate be
MakeDate (MakeDay (YearFromTime (tv),MonthFromTime (tv), dt),TimeWithinDay (tv)). - Let u be
TimeClip (UTC (newDate)). - Set dateObj.[[DateValue]] to u.
- Return u.
21.4.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let y be ?
ToNumber (year). - If tv is
NaN , set tv to+0 𝔽; else set tv toLocalTime (tv). - If month is present, let m be ?
ToNumber (month); else let m beMonthFromTime (tv). - If date is present, let dt be ?
ToNumber (date); else let dt beDateFromTime (tv). - Let newDate be
MakeDate (MakeDay (y, m, dt),TimeWithinDay (tv)). - Let u be
TimeClip (UTC (newDate)). - Set dateObj.[[DateValue]] to u.
- Return u.
The
If month is not present, this method behaves as if month was present with the value getMonth(). If date is not present, it behaves as if date was present with the value getDate().
21.4.4.22 Date.prototype.setHours ( hour [ , min [ , sec [ , ms ] ] ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let h be ?
ToNumber (hour). - If min is present, let m be ?
ToNumber (min). - If sec is present, let s be ?
ToNumber (sec). - If ms is present, let milli be ?
ToNumber (ms). - If tv is
NaN , returnNaN . - Set tv to
LocalTime (tv). - If min is not present, let m be
MinFromTime (tv). - If sec is not present, let s be
SecFromTime (tv). - If ms is not present, let milli be
msFromTime (tv). - Let date be
MakeDate (Day (tv),MakeTime (h, m, s, milli)). - Let u be
TimeClip (UTC (date)). - Set dateObj.[[DateValue]] to u.
- Return u.
The
If min is not present, this method behaves as if min was present with the value getMinutes(). If sec is not present, it behaves as if sec was present with the value getSeconds(). If ms is not present, it behaves as if ms was present with the value getMilliseconds().
21.4.4.23 Date.prototype.setMilliseconds ( ms )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Set ms to ?
ToNumber (ms). - If tv is
NaN , returnNaN . - Set tv to
LocalTime (tv). - Let time be
MakeTime (HourFromTime (tv),MinFromTime (tv),SecFromTime (tv), ms). - Let u be
TimeClip (UTC (MakeDate (Day (tv), time))). - Set dateObj.[[DateValue]] to u.
- Return u.
21.4.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let m be ?
ToNumber (min). - If sec is present, let s be ?
ToNumber (sec). - If ms is present, let milli be ?
ToNumber (ms). - If tv is
NaN , returnNaN . - Set tv to
LocalTime (tv). - If sec is not present, let s be
SecFromTime (tv). - If ms is not present, let milli be
msFromTime (tv). - Let date be
MakeDate (Day (tv),MakeTime (HourFromTime (tv), m, s, milli)). - Let u be
TimeClip (UTC (date)). - Set dateObj.[[DateValue]] to u.
- Return u.
The
If sec is not present, this method behaves as if sec was present with the value getSeconds(). If ms is not present, this behaves as if ms was present with the value getMilliseconds().
21.4.4.25 Date.prototype.setMonth ( month [ , date ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let m be ?
ToNumber (month). - If date is present, let dt be ?
ToNumber (date). - If tv is
NaN , returnNaN . - Set tv to
LocalTime (tv). - If date is not present, let dt be
DateFromTime (tv). - Let newDate be
MakeDate (MakeDay (YearFromTime (tv), m, dt),TimeWithinDay (tv)). - Let u be
TimeClip (UTC (newDate)). - Set dateObj.[[DateValue]] to u.
- Return u.
The
If date is not present, this method behaves as if date was present with the value getDate().
21.4.4.26 Date.prototype.setSeconds ( sec [ , ms ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let s be ?
ToNumber (sec). - If ms is present, let milli be ?
ToNumber (ms). - If tv is
NaN , returnNaN . - Set tv to
LocalTime (tv). - If ms is not present, let milli be
msFromTime (tv). - Let date be
MakeDate (Day (tv),MakeTime (HourFromTime (tv),MinFromTime (tv), s, milli)). - Let u be
TimeClip (UTC (date)). - Set dateObj.[[DateValue]] to u.
- Return u.
The
If ms is not present, this method behaves as if ms was present with the value getMilliseconds().
21.4.4.27 Date.prototype.setTime ( time )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let t be ?
ToNumber (time). - Let v be
TimeClip (t). - Set dateObj.[[DateValue]] to v.
- Return v.
21.4.4.28 Date.prototype.setUTCDate ( date )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let dt be ?
ToNumber (date). - If tv is
NaN , returnNaN . - Let newDate be
MakeDate (MakeDay (YearFromTime (tv),MonthFromTime (tv), dt),TimeWithinDay (tv)). - Let v be
TimeClip (newDate). - Set dateObj.[[DateValue]] to v.
- Return v.
21.4.4.29 Date.prototype.setUTCFullYear ( year [ , month [ , date ] ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , set tv to+0 𝔽. - Let y be ?
ToNumber (year). - If month is present, let m be ?
ToNumber (month); else let m beMonthFromTime (tv). - If date is present, let dt be ?
ToNumber (date); else let dt beDateFromTime (tv). - Let newDate be
MakeDate (MakeDay (y, m, dt),TimeWithinDay (tv)). - Let v be
TimeClip (newDate). - Set dateObj.[[DateValue]] to v.
- Return v.
The
If month is not present, this method behaves as if month was present with the value getUTCMonth(). If date is not present, it behaves as if date was present with the value getUTCDate().
21.4.4.30 Date.prototype.setUTCHours ( hour [ , min [ , sec [ , ms ] ] ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let h be ?
ToNumber (hour). - If min is present, let m be ?
ToNumber (min). - If sec is present, let s be ?
ToNumber (sec). - If ms is present, let milli be ?
ToNumber (ms). - If tv is
NaN , returnNaN . - If min is not present, let m be
MinFromTime (tv). - If sec is not present, let s be
SecFromTime (tv). - If ms is not present, let milli be
msFromTime (tv). - Let date be
MakeDate (Day (tv),MakeTime (h, m, s, milli)). - Let v be
TimeClip (date). - Set dateObj.[[DateValue]] to v.
- Return v.
The
If min is not present, this method behaves as if min was present with the value getUTCMinutes(). If sec is not present, it behaves as if sec was present with the value getUTCSeconds(). If ms is not present, it behaves as if ms was present with the value getUTCMilliseconds().
21.4.4.31 Date.prototype.setUTCMilliseconds ( ms )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Set ms to ?
ToNumber (ms). - If tv is
NaN , returnNaN . - Let time be
MakeTime (HourFromTime (tv),MinFromTime (tv),SecFromTime (tv), ms). - Let v be
TimeClip (MakeDate (Day (tv), time)). - Set dateObj.[[DateValue]] to v.
- Return v.
21.4.4.32 Date.prototype.setUTCMinutes ( min [ , sec [ , ms ] ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let m be ?
ToNumber (min). - If sec is present, let s be ?
ToNumber (sec). - If ms is present, let milli be ?
ToNumber (ms). - If tv is
NaN , returnNaN . - If sec is not present, let s be
SecFromTime (tv). - If ms is not present, let milli be
msFromTime (tv). - Let date be
MakeDate (Day (tv),MakeTime (HourFromTime (tv), m, s, milli)). - Let v be
TimeClip (date). - Set dateObj.[[DateValue]] to v.
- Return v.
The
If sec is not present, this method behaves as if sec was present with the value getUTCSeconds(). If ms is not present, it behaves as if ms was present with the value return by getUTCMilliseconds().
21.4.4.33 Date.prototype.setUTCMonth ( month [ , date ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let m be ?
ToNumber (month). - If date is present, let dt be ?
ToNumber (date). - If tv is
NaN , returnNaN . - If date is not present, let dt be
DateFromTime (tv). - Let newDate be
MakeDate (MakeDay (YearFromTime (tv), m, dt),TimeWithinDay (tv)). - Let v be
TimeClip (newDate). - Set dateObj.[[DateValue]] to v.
- Return v.
The
If date is not present, this method behaves as if date was present with the value getUTCDate().
21.4.4.34 Date.prototype.setUTCSeconds ( sec [ , ms ] )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Let s be ?
ToNumber (sec). - If ms is present, let milli be ?
ToNumber (ms). - If tv is
NaN , returnNaN . - If ms is not present, let milli be
msFromTime (tv). - Let date be
MakeDate (Day (tv),MakeTime (HourFromTime (tv),MinFromTime (tv), s, milli)). - Let v be
TimeClip (date). - Set dateObj.[[DateValue]] to v.
- Return v.
The
If ms is not present, this method behaves as if ms was present with the value getUTCMilliseconds().
21.4.4.35 Date.prototype.toDateString ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , return"Invalid Date" . - Let t be
LocalTime (tv). - Return
DateString (t).
21.4.4.36 Date.prototype.toISOString ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , throw aRangeError exception. Assert : tv is anintegral Number .- If tv corresponds with a year that cannot be represented in the
Date Time String Format , throw aRangeError exception. - Return a String representation of tv in the
Date Time String Format on the UTC time scale, including all format elements and the UTC offset representation"Z" .
21.4.4.37 Date.prototype.toJSON ( key )
This method provides a String representation of a Date for use by JSON.stringify (
It performs the following steps when called:
- Let obj be ?
ToObject (this value). - Let tv be ?
ToPrimitive (obj,number ). - If tv
is a Number and tv is notfinite , returnnull . - Return ?
Invoke (obj,"toISOString" ).
The argument is ignored.
This method is intentionally generic; it does not require that its toISOString method.
21.4.4.38 Date.prototype.toLocaleDateString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method returns a String value. The contents of the String are
The meaning of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
21.4.4.39 Date.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method returns a String value. The contents of the String are
The meaning of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
21.4.4.40 Date.prototype.toLocaleTimeString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method returns a String value. The contents of the String are
The meaning of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
21.4.4.41 Date.prototype.toString ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- Return
ToDateString (tv).
For any Date d such that d.[[DateValue]] is evenly divisible by 1000, the result of Date.parse(d.toString()) = d.valueOf(). See
This method is not generic; it throws a
21.4.4.41.1 TimeString ( tv )
The abstract operation TimeString takes argument tv (a Number, but not
- Let hour be
ToZeroPaddedDecimalString (ℝ (HourFromTime (tv)), 2). - Let minute be
ToZeroPaddedDecimalString (ℝ (MinFromTime (tv)), 2). - Let second be
ToZeroPaddedDecimalString (ℝ (SecFromTime (tv)), 2). - Return the
string-concatenation of hour,":" , minute,":" , second, the code unit 0x0020 (SPACE), and"GMT" .
21.4.4.41.2 DateString ( tv )
The abstract operation DateString takes argument tv (a Number, but not
- Let weekday be the Name of the entry in
Table 60 with the NumberWeekDay (tv). - Let month be the Name of the entry in
Table 61 with the NumberMonthFromTime (tv). - Let day be
ToZeroPaddedDecimalString (ℝ (DateFromTime (tv)), 2). - Let yv be
YearFromTime (tv). - If yv is
+0 𝔽 or yv >+0 𝔽, let yearSign be the empty String; else let yearSign be"-" . - Let paddedYear be
ToZeroPaddedDecimalString (abs (ℝ (yv)), 4). - Return the
string-concatenation of weekday, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), yearSign, and paddedYear.
| Number | Name |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Number | Name |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21.4.4.41.3 TimeZoneString ( tv )
The abstract operation TimeZoneString takes argument tv (an
- Let systemTimeZoneIdentifier be
SystemTimeZoneIdentifier (). - If
IsTimeZoneOffsetString (systemTimeZoneIdentifier) istrue , then- Let offsetNs be
ParseTimeZoneOffsetString (systemTimeZoneIdentifier).
- Let offsetNs be
- Else,
- Let offsetNs be
GetNamedTimeZoneOffsetNanoseconds (systemTimeZoneIdentifier,ℤ (ℝ (tv) × 106)).
- Let offsetNs be
- Let offset be
𝔽 (truncate (offsetNs / 106)). - If offset is
+0 𝔽 or offset >+0 𝔽, then- Let offsetSign be
"+" . - Let absOffset be offset.
- Let offsetSign be
- Else,
- Let offsetSign be
"-" . - Let absOffset be -offset.
- Let offsetSign be
- Let offsetMin be
ToZeroPaddedDecimalString (ℝ (MinFromTime (absOffset)), 2). - Let offsetHour be
ToZeroPaddedDecimalString (ℝ (HourFromTime (absOffset)), 2). - Let tzName be an
implementation-defined string that is either the empty String or thestring-concatenation of the code unit 0x0020 (SPACE), the code unit 0x0028 (LEFT PARENTHESIS), animplementation-defined timezone name, and the code unit 0x0029 (RIGHT PARENTHESIS). - Return the
string-concatenation of offsetSign, offsetHour, offsetMin, and tzName.
21.4.4.41.4 ToDateString ( tv )
The abstract operation ToDateString takes argument tv (an
- If tv is
NaN , return"Invalid Date" . - Let localTime be
LocalTime (tv). - Return the
string-concatenation ofDateString (localTime), the code unit 0x0020 (SPACE),TimeString (localTime), andTimeZoneString (tv).
21.4.4.42 Date.prototype.toTimeString ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , return"Invalid Date" . - Let localTime be
LocalTime (tv). - Return the
string-concatenation ofTimeString (localTime) andTimeZoneString (tv).
21.4.4.43 Date.prototype.toUTCString ( )
This method returns a String value representing the instant in time corresponding to the HTTP-date from RFC 7231, generalized to support the full range of times supported by ECMAScript Dates.
It performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Let tv be dateObj.[[DateValue]].
- If tv is
NaN , return"Invalid Date" . - Let weekday be the Name of the entry in
Table 60 with the NumberWeekDay (tv). - Let month be the Name of the entry in
Table 61 with the NumberMonthFromTime (tv). - Let day be
ToZeroPaddedDecimalString (ℝ (DateFromTime (tv)), 2). - Let yv be
YearFromTime (tv). - If yv is
+0 𝔽 or yv >+0 𝔽, let yearSign be the empty String; else let yearSign be"-" . - Let paddedYear be
ToZeroPaddedDecimalString (abs (ℝ (yv)), 4). - Return the
string-concatenation of weekday,"," , the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), yearSign, paddedYear, the code unit 0x0020 (SPACE), andTimeString (tv).
21.4.4.44 Date.prototype.valueOf ( )
This method performs the following steps when called:
- Let dateObj be the
this value. - Perform ?
RequireInternalSlot (dateObj, [[DateValue]]). - Return dateObj.[[DateValue]].
21.4.4.45 Date.prototype [ %Symbol.toPrimitive% ] ( hint )
This method is called by ECMAScript language operators to convert a Date to a primitive value. The allowed values for hint are
It performs the following steps when called:
- Let obj be the
this value. - If obj
is not an Object , throw aTypeError exception. - If hint is either
"string" or"default" , then- Let tryFirst be
string .
- Let tryFirst be
- Else if hint is
"number" , then- Let tryFirst be
number .
- Let tryFirst be
- Else,
- Throw a
TypeError exception.
- Throw a
- Return ?
OrdinaryToPrimitive (obj, tryFirst).
This property has the attributes { [[Writable]]:
The value of the
21.4.5 Properties of Date Instances
Date instances are