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 aconstructor . - is designed to be subclassable. It may be used as the value of an
extendsclause of a class definition. Subclass constructors 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 )
When Number is called with argument value, the following steps are taken:
- If value is present, then
- Else,
- Let n be
+0 𝔽.
- Let n be
- If NewTarget is
undefined , return n. - Let O be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[NumberData]] »).%Number.prototype% " Set O.[[NumberData]] to n.- Return O.
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 )
When Number.isFinite is called with one argument number, the following steps are taken:
- If
Type (number) is not Number, returnfalse . - If number is
NaN ,+∞ 𝔽, or-∞ 𝔽, returnfalse . - Otherwise, return
true .
21.1.2.3 Number.isInteger ( number )
When Number.isInteger is called with one argument number, the following steps are taken:
- Return !
IsIntegralNumber (number).
21.1.2.4 Number.isNaN ( number )
When Number.isNaN is called with one argument number, the following steps are taken:
- If
Type (number) is not Number, returnfalse . - If number is
NaN , returntrue . - Otherwise, return
false .
This function differs from the global isNaN function (
21.1.2.5 Number.isSafeInteger ( number )
When Number.isSafeInteger is called with one argument number, the following steps are taken:
- If !
IsIntegralNumber (number) istrue , then- If
abs (ℝ(number)) ≤ 253 - 1, returntrue .
- If
- Return
false .
21.1.2.6 Number.MAX_SAFE_INTEGER
The value of Number.MAX_SAFE_INTEGER is the largest
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 finite value of the Number
This property has the attributes { [[Writable]]:
21.1.2.8 Number.MIN_SAFE_INTEGER
The value of Number.MIN_SAFE_INTEGER is the smallest
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 Number
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 value of the Number.parseFloat
21.1.2.13 Number.parseInt ( string, radix )
The value of the Number.parseInt
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 abstract operation thisNumberValue takes argument value. It performs the following steps when called:
The phrase “this
21.1.3.1 Number.prototype.constructor
The initial value of Number.prototype.constructor is
21.1.3.2 Number.prototype.toExponential ( fractionDigits )
Return a String containing this
- Let x be ?
thisNumberValue (this value). - Let f be ?
ToIntegerOrInfinity (fractionDigits). Assert : If fractionDigits isundefined , then f is 0.- If x is not finite, return !
Number::toString (x). - If f < 0 or f > 100, throw a
RangeError exception. Set x to ℝ(x).- Let s be the empty String.
- If x < 0, then
- If x = 0, then
- Let m be the String value consisting of f + 1 occurrences of the code unit 0x0030 (DIGIT ZERO).
- Let e be 0.
- Else,
- If fractionDigits is not
undefined , then- Let e and n be integers such that 10f ≤ n < 10f + 1 and for which n × 10e - n - x is as close to zero as possible. If there are two such sets of e and n, pick the e and n for which n × 10e - f is larger.
- Else,
- Let e, n, and f be integers such that f ≥ 0, 10f ≤ n < 10f + 1, n × 10e - f is x, and f is as small as possible. Note that the decimal representation of n has f + 1 digits, n is not divisible by 10, and the least significant digit of n is not necessarily uniquely determined by these criteria.
- Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
- If fractionDigits is not
- If f ≠ 0, then
- Let a be the first code unit of m.
- Let b be the other f code units of m.
Set m to thestring-concatenation of a,"." , and b.
- If e = 0, then
- Let c be
"+" . - Let d be
"0" .
- Let c be
- Else,
Set m to thestring-concatenation of m,"e" , c, and d.- Return the
string-concatenation of s and m.
For implementations that provide more accurate conversions than required by the rules above, it is recommended that the following alternative version of step
- Let e, n, and f be integers such that f ≥ 0, 10f ≤ n < 10f + 1, n × 10e - f is x, and f is as small as possible. If there are multiple possibilities for n, choose the value of n for which n × 10e - f is closest in value to x. If there are two such possible values of n, choose the one that is even.
21.1.3.3 Number.prototype.toFixed ( fractionDigits )
toFixed returns a String containing this
The following steps are performed:
- Let x be ?
thisNumberValue (this value). - Let f be ?
ToIntegerOrInfinity (fractionDigits). Assert : If fractionDigits isundefined , then f is 0.- If f is not finite, throw a
RangeError exception. - If f < 0 or f > 100, throw a
RangeError exception. - If x is not finite, return !
Number::toString (x). Set x to ℝ(x).- Let s be the empty String.
- If x < 0, then
- If x ≥ 1021, then
- Let m be !
ToString (𝔽(x)).
- Let m be !
- Else,
- Let n be an
integer for which n / 10f - x is as close to zero as possible. If there are two such n, pick the larger n. - If n = 0, let m be the String
"0" . Otherwise, let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes). - If f ≠ 0, then
- Let k be the length of m.
- If k ≤ f, then
- Let z be the String value consisting of f + 1 - k occurrences of the code unit 0x0030 (DIGIT ZERO).
Set m to thestring-concatenation of z and m.Set k to f + 1.
- Let a be the first k - f code units of m.
- Let b be the other f code units of m.
Set m to thestring-concatenation of a,"." , and b.
- Let n be an
- Return the
string-concatenation of s and m.
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 the Number.prototype.toLocaleString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleString method is used.
Produces a String value that represents this 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 )
Return a String containing this
- Let x be ?
thisNumberValue (this value). - If precision is
undefined , return !ToString (x). - Let p be ?
ToIntegerOrInfinity (precision). - If x is not finite, return !
Number::toString (x). - If p < 1 or p > 100, throw a
RangeError exception. Set x to ℝ(x).- Let s be the empty String.
- If x < 0, then
- If x = 0, then
- Let m be the String value consisting of p occurrences of the code unit 0x0030 (DIGIT ZERO).
- Let e be 0.
- Else,
- Let e and n be integers such that 10p - 1 ≤ n < 10p and for which n × 10e - p + 1 - x is as close to zero as possible. If there are two such sets of e and n, pick the e and n for which n × 10e - p + 1 is larger.
- Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
- If e < -6 or e ≥ p, then
Assert : e ≠ 0.- If p ≠ 1, then
- Let a be the first code unit of m.
- Let b be the other p - 1 code units of m.
Set m to thestring-concatenation of a,"." , and b.
- If e > 0, then
- Let c be the code unit 0x002B (PLUS SIGN).
- Else,
- Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes).
- Return the
string-concatenation of s, m, the code unit 0x0065 (LATIN SMALL LETTER E), c, and d.
- If e = p - 1, return the
string-concatenation of s and m. - If e ≥ 0, then
Set m to thestring-concatenation of the first e + 1 code units of m, the code unit 0x002E (FULL STOP), and the remaining p - (e + 1) code units of m.
- Else,
Set m to thestring-concatenation of the code unit 0x0030 (DIGIT ZERO), the code unit 0x002E (FULL STOP), -(e + 1) occurrences of the code unit 0x0030 (DIGIT ZERO), and the String m.
- Return the
string-concatenation of s and m.
21.1.3.6 Number.prototype.toString ( [ radix ] )
The optional radix should be an
The following steps are performed:
- Let x be ?
thisNumberValue (this value). - If radix is
undefined , let radixMV be 10. - Else, let radixMV be ?
ToIntegerOrInfinity (radix). - If radixMV < 2 or radixMV > 36, throw a
RangeError exception. - If radixMV = 10, return !
ToString (x). - Return the String representation of this
Number value using the radix specified by radixMV. Lettersa-zare used for digits with values 10 through 35. The precise algorithm isimplementation-defined , however the algorithm should be a generalization of that specified in6.1.6.1.20 .
The toString function is not generic; it throws a
The toString method is
21.1.3.7 Number.prototype.valueOf ( )
- Return ?
thisNumberValue (this value).
21.1.4 Properties of Number Instances
Number instances are ordinary objects that inherit properties from the
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 aconstructor . - 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 )
When BigInt is called with argument value, the following steps are taken:
- If NewTarget is not
undefined , throw aTypeError exception. - Let prim be ?
ToPrimitive (value,number ). - If
Type (prim) is Number, return ?NumberToBigInt (prim). - Otherwise, return ?
ToBigInt (value).
21.2.1.1.1 NumberToBigInt ( number )
The abstract operation NumberToBigInt takes argument number (a Number). It performs the following steps when called:
- If
IsIntegralNumber (number) isfalse , throw aRangeError exception. - Return the BigInt value that represents ℝ(number).
21.2.2 Properties of the BigInt Constructor
The value of the [[Prototype]] internal slot of the BigInt
The BigInt
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 abstract operation thisBigIntValue takes argument value. It performs the following steps when called:
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 the BigInt.prototype.toLocaleString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleString method is used.
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
The following steps are performed:
- Let x be ?
thisBigIntValue (this value). - If radix is
undefined , let radixMV be 10. - Else, let radixMV be ?
ToIntegerOrInfinity (radix). - If radixMV < 2 or radixMV > 36, throw a
RangeError exception. - If radixMV = 10, return !
ToString (x). - Return the String representation of this
Number value using the radix specified by radixMV. Lettersa-zare used for digits with values 10 through 35. The precise algorithm isimplementation-defined , however the algorithm should be a generalization of that specified in6.1.6.2.23 .
The toString function is not generic; it throws a
21.2.3.4 BigInt.prototype.valueOf ( )
- Return ?
thisBigIntValue (this value).
21.2.3.5 BigInt.prototype [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value
This property has the attributes { [[Writable]]:
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 [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value
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, sqrt, 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 )
Returns the absolute value of x; the result has the same magnitude as x but has positive sign.
When the Math.abs method is called with argument x, the following steps are taken:
- 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 )
Returns the inverse cosine of x. The result is expressed in radians and ranges from
When the Math.acos method is called with argument x, the following steps are taken:
- 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 value representing the result of the inverse cosine of ℝ(n).
21.3.2.3 Math.acosh ( x )
Returns the inverse hyperbolic cosine of x.
When the Math.acosh method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN or n is+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n <
1 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the inverse hyperbolic cosine of ℝ(n).
21.3.2.4 Math.asin ( x )
Returns the inverse sine of x. The result is expressed in radians and ranges from 𝔽(-π / 2) to 𝔽(π / 2), inclusive.
When the Math.asin method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n >
1 𝔽 or n <-1 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the inverse sine of ℝ(n).
21.3.2.5 Math.asinh ( x )
Returns the inverse hyperbolic sine of x.
When the Math.asinh method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, n is+∞ 𝔽, or n is-∞ 𝔽, return n. - Return an
implementation-approximated value representing the result of the inverse hyperbolic sine of ℝ(n).
21.3.2.6 Math.atan ( x )
Returns the inverse tangent of x. The result is expressed in radians and ranges from 𝔽(-π / 2) to 𝔽(π / 2), inclusive.
When the Math.atan method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n is
+∞ 𝔽, return animplementation-approximated value representing π / 2. - If n is
-∞ 𝔽, return animplementation-approximated value representing -π / 2. - Return an
implementation-approximated value representing the result of the inverse tangent of ℝ(n).
21.3.2.7 Math.atanh ( x )
Returns the inverse hyperbolic tangent of x.
When the Math.atanh method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n >
1 𝔽 or n <-1 𝔽, returnNaN . - If n is
1 𝔽, return+∞ 𝔽. - If n is
-1 𝔽, return-∞ 𝔽. - Return an
implementation-approximated value representing the result of the inverse hyperbolic tangent of ℝ(n).
21.3.2.8 Math.atan2 ( y, x )
Returns the inverse tangent of the quotient
When the Math.atan2 method is called with arguments y and x, the following steps are taken:
- 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 value representing π / 4. - If nx is
-∞ 𝔽, return animplementation-approximated value representing 3π / 4. - Return an
implementation-approximated value representing π / 2.
- If nx is
- If ny is
-∞ 𝔽, then- If nx is
+∞ 𝔽, return animplementation-approximated value representing -π / 4. - If nx is
-∞ 𝔽, return animplementation-approximated value representing -3π / 4. - Return an
implementation-approximated value representing -π / 2.
- If nx is
- If ny is
+0 𝔽, then- If nx >
+0 𝔽 or nx is+0 𝔽, return+0 𝔽. - Return an
implementation-approximated value representing π.
- If nx >
- If ny is
-0 𝔽, then- If nx >
+0 𝔽 or nx is+0 𝔽, return-0 𝔽. - Return an
implementation-approximated value representing -π.
- If nx >
Assert : ny is finite and is neither+0 𝔽 nor-0 𝔽.- If ny >
+0 𝔽, then- If nx is
+∞ 𝔽, return+0 𝔽. - If nx is
-∞ 𝔽, return animplementation-approximated value representing π. - If nx is
+0 𝔽 or nx is-0 𝔽, return animplementation-approximated value representing π / 2.
- If nx is
- If ny <
+0 𝔽, then- If nx is
+∞ 𝔽, return-0 𝔽. - If nx is
-∞ 𝔽, return animplementation-approximated value representing -π. - If nx is
+0 𝔽 or nx is-0 𝔽, return animplementation-approximated value representing -π / 2.
- If nx is
Assert : nx is finite and is neither+0 𝔽 nor-0 𝔽.- Return an
implementation-approximated value representing the result of the inverse tangent of the quotient ℝ(ny) / ℝ(nx).
21.3.2.9 Math.cbrt ( x )
Returns the cube root of x.
When the Math.cbrt method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, n is+∞ 𝔽, or n is-∞ 𝔽, return n. - Return an
implementation-approximated value representing the result of the cube root of ℝ(n).
21.3.2.10 Math.ceil ( x )
Returns the smallest (closest to -∞)
When the Math.ceil method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, n is+∞ 𝔽, or n is-∞ 𝔽, 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 )
When the Math.clz32 method is called with argument x, the following steps are taken:
- Let n be ?
ToUint32 (x). - Let p be the number of leading zero bits in the unsigned 32-bit binary representation of n.
- Return 𝔽(p).
If n is
21.3.2.12 Math.cos ( x )
Returns the cosine of x. The argument is expressed in radians.
When the Math.cos method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n is
+∞ 𝔽 or n is-∞ 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the cosine of ℝ(n).
21.3.2.13 Math.cosh ( x )
Returns the hyperbolic cosine of x.
When the Math.cosh method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+∞ 𝔽, or n is-∞ 𝔽, return n. - If n is
+0 𝔽 or n is-0 𝔽, return1 𝔽. - Return an
implementation-approximated value representing the result of 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 )
Returns the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
When the Math.exp method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN or n is+∞ 𝔽, return n. - If n is
+0 𝔽 or n is-0 𝔽, return1 𝔽. - If n is
-∞ 𝔽, return+0 𝔽. - Return an
implementation-approximated value representing the result of the exponential function of ℝ(n).
21.3.2.15 Math.expm1 ( x )
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.
When the Math.expm1 method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, or n is+∞ 𝔽, return n. - If n is
-∞ 𝔽, return-1 𝔽. - Return an
implementation-approximated value representing the result of subtracting 1 from the exponential function of ℝ(n).
21.3.2.16 Math.floor ( x )
Returns the greatest (closest to +∞)
When the Math.floor method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, n is+∞ 𝔽, or n is-∞ 𝔽, 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 )
When the Math.fround method is called with argument x, the following steps are taken:
- 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 a value in
IEEE 754-2019 binary32 format using roundTiesToEven mode. - Let n64 be the result of converting n32 to a value in
IEEE 754-2019 binary64 format. - Return the ECMAScript
Number value corresponding to n64.
21.3.2.18 Math.hypot ( ...args )
Returns the square root of the sum of squares of its arguments.
When the Math.hypot method is called with zero or more arguments which form the rest parameter ...args, the following steps are taken:
- 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
+∞ 𝔽 or number is-∞ 𝔽, return+∞ 𝔽.
- If number is
- 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 value representing the square root of the sum of squares of the mathematical values of the elements of coerced.
The hypot method is
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.19 Math.imul ( x, y )
21.3.2.20 Math.log ( x )
Returns the natural logarithm of x.
When the Math.log method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN or n is+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is
+0 𝔽 or n is-0 𝔽, return-∞ 𝔽. - If n <
+0 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the natural logarithm of ℝ(n).
21.3.2.21 Math.log1p ( x )
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.
When the Math.log1p method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, or n is+∞ 𝔽, return n. - If n is
-1 𝔽, return-∞ 𝔽. - If n <
-1 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the natural logarithm of 1 + ℝ(n).
21.3.2.22 Math.log10 ( x )
Returns the base 10 logarithm of x.
When the Math.log10 method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN or n is+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is
+0 𝔽 or n is-0 𝔽, return-∞ 𝔽. - If n <
+0 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the base 10 logarithm of ℝ(n).
21.3.2.23 Math.log2 ( x )
Returns the base 2 logarithm of x.
When the Math.log2 method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN or n is+∞ 𝔽, return n. - If n is
1 𝔽, return+0 𝔽. - If n is
+0 𝔽 or n is-0 𝔽, return-∞ 𝔽. - If n <
+0 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the base 2 logarithm of ℝ(n).
21.3.2.24 Math.max ( ...args )
Given zero or more arguments, calls
When the Math.max method is called with zero or more arguments which form the rest parameter ...args, the following steps are taken:
- 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 max method is
21.3.2.25 Math.min ( ...args )
Given zero or more arguments, calls
When the Math.min method is called with zero or more arguments which form the rest parameter ...args, the following steps are taken:
- 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 largest value is done using the
The min method is
21.3.2.26 Math.pow ( base, exponent )
21.3.2.27 Math.random ( )
Returns a
Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls.
21.3.2.28 Math.round ( x )
Returns the
When the Math.round method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN ,+∞ 𝔽,-∞ 𝔽, or 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 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.29 Math.sign ( x )
Returns the sign of x, indicating whether x is positive, negative, or zero.
When the Math.sign method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n <
+0 𝔽, return-1 𝔽. - Return
1 𝔽.
21.3.2.30 Math.sin ( x )
Returns the sine of x. The argument is expressed in radians.
When the Math.sin method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n is
+∞ 𝔽 or n is-∞ 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the sine of ℝ(n).
21.3.2.31 Math.sinh ( x )
Returns the hyperbolic sine of x.
When the Math.sinh method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, n is+∞ 𝔽, or n is-∞ 𝔽, return n. - Return an
implementation-approximated value representing the result of 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.32 Math.sqrt ( x )
Returns the square root of x.
When the Math.sqrt method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, or n is+∞ 𝔽, return n. - If n <
+0 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the square root of ℝ(n).
21.3.2.33 Math.tan ( x )
Returns the tangent of x. The argument is expressed in radians.
When the Math.tan method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n is
+∞ 𝔽, or n is-∞ 𝔽, returnNaN . - Return an
implementation-approximated value representing the result of the tangent of ℝ(n).
21.3.2.34 Math.tanh ( x )
Returns the hyperbolic tangent of x.
When the Math.tanh method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, or n is-0 𝔽, return n. - If n is
+∞ 𝔽, return1 𝔽. - If n is
-∞ 𝔽, return-1 𝔽. - Return an
implementation-approximated value representing the result of 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.35 Math.trunc ( x )
Returns the integral part of the number x, removing any fractional digits. If x is already integral, the result is x.
When the Math.trunc method is called with argument x, the following steps are taken:
- Let n be ?
ToNumber (x). - If n is
NaN , n is+0 𝔽, n is-0 𝔽, n is+∞ 𝔽, or n is-∞ 𝔽, 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 functions are
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 is a Number, either a finite
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 integers from -9,007,199,254,740,992 to 9,007,199,254,740,992 (
The exact moment of midnight at the beginning of 1 January 1970 UTC is represented by the time value
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 Day Number and Time within Day
A given
where the number of milliseconds per day is
The remainder is called the time within the day:
21.4.1.3 Year Number
ECMAScript uses a proleptic Gregorian calendar to map a day number to a year number and to determine the month and date within that year. In this calendar, leap years are precisely those which are (divisible by 4) and ((not divisible by 100) or (divisible by 400)). The number of days in year number y is therefore defined by
All non-leap years have 365 days with the usual number of days per month and leap years have an extra day in February. The day number of the first day of year y is given by:
The
A
The leap-year function is
21.4.1.4 Month Number
Months are identified by an
where
A month value of
21.4.1.5 Date Number
A date number is identified by an
21.4.1.6 Week Day
The weekday for a particular
A weekday value of
21.4.1.7 LocalTZA ( t, isUTC )
LocalTZA( t, isUTC ) is an
When isUTC is true,
When isUTC is false,
Input t is nominally a
When
If an implementation does not support a conversion described above or if political rules for time t are not available within the implementation, the result must be
It is recommended that implementations use the time zone information of the IANA Time Zone Database https://www.iana.org/time-zones/.
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. LocalTZA(
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). LocalTZA(
Local time zone offset values may be positive or negative.
21.4.1.8 LocalTime ( t )
The abstract operation LocalTime takes argument t. It converts t from UTC to local time. It performs the following steps when called:
- Return t +
LocalTZA (t,true ).
Two different input time values
21.4.1.9 UTC ( t )
The abstract operation UTC takes argument t. It converts t from local time to UTC. It performs the following steps when called:
- Return t -
LocalTZA (t,false ).
21.4.1.10 Hours, Minutes, Second, and Milliseconds
The following
where
21.4.1.11 MakeTime ( hour, min, sec, ms )
The abstract operation MakeTime takes arguments hour (a Number), min (a Number), sec (a Number), and ms (a Number). It calculates a number of milliseconds. It performs the following steps when called:
- If hour is not finite or min is not finite or sec is not finite or ms is not finite, return
NaN . - Let h be 𝔽(!
ToIntegerOrInfinity (hour)). - Let m be 𝔽(!
ToIntegerOrInfinity (min)). - Let s be 𝔽(!
ToIntegerOrInfinity (sec)). - Let milli be 𝔽(!
ToIntegerOrInfinity (ms)). - Let t be ((h
*msPerHour +m*msPerMinute )+s*msPerSecond )+milli, performing the arithmetic according toIEEE 754-2019 rules (that is, as if using the ECMAScript operators*and+). - Return t.
21.4.1.12 MakeDay ( year, month, date )
The abstract operation MakeDay takes arguments year (a Number), month (a Number), and date (a Number). It calculates a number of days. It performs the following steps when called:
- If year is not finite or month is not finite or date is not finite, return
NaN . - 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, return
NaN . - Let mn be 𝔽(ℝ(m)
modulo 12). - Find a finite
time value t such thatYearFromTime (t) is ym andMonthFromTime (t) is mn and DateFromTime(t) is1 𝔽; but if this is not possible (because some argument is out of range), returnNaN . - Return
Day (t) + dt -1 𝔽.
21.4.1.13 MakeDate ( day, time )
The abstract operation MakeDate takes arguments day (a Number) and time (a Number). It calculates a number of milliseconds. It performs the following steps when called:
- If day is not finite or time is not finite, return
NaN . - Let tv be day ×
msPerDay + time. - If tv is not finite, return
NaN . - Return tv.
21.4.1.14 TimeClip ( time )
The abstract operation TimeClip takes argument time (a Number). It calculates a number of milliseconds. It performs the following steps when called:
- If time is not finite, return
NaN . - If
abs (ℝ(time)) > 8.64 × 1015, returnNaN . - Return 𝔽(!
ToIntegerOrInfinity (time)).
21.4.1.15 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 (indicating local time ahead of or behind UTC, respectively)
|
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.15.1 Expanded Years
Date.parse
Examples of date-time values with expanded years:
| -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.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 object 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.
- is designed to be subclassable. It may be used as the value of an
extendsclause of a class definition. Subclass constructors 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. - has a
"length" property whose value is7 𝔽.
21.4.2.1 Date ( ...values )
When the Date function is called, the following steps are taken:
- 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
Type (value) is Object and value has a [[DateValue]] internal slot, then- Let tv be !
thisTimeValue (value).
- Let tv be !
- Else,
- Let v be ?
ToPrimitive (value). - If
Type (v) is String, thenAssert : The next step never returns anabrupt completion becauseType (v) is 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 𝔽. - If y is
NaN , let yr beNaN . - Else,
- Let yi be !
ToIntegerOrInfinity (y). - If 0 ≤ yi ≤ 99, let yr be
1900 𝔽 + 𝔽(yi); otherwise, let yr be y.
- Let yi be !
- Let finalDate be
MakeDate (MakeDay (yr, m, dt),MakeTime (h, min, s, milli)). - Let dv be
TimeClip (UTC (finalDate)).
- Let O be ?
OrdinaryCreateFromConstructor (NewTarget," , « [[DateValue]] »).%Date.prototype% " Set O.[[DateValue]] to dv.- Return O.
21.4.3 Properties of the Date Constructor
The Date
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
21.4.3.1 Date.now ( )
The now function returns the now.
21.4.3.2 Date.parse ( string )
The parse function applies the parse interprets the resulting String as a date and time; it returns a Number, the UTC Date.parse to return
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 object 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 Date.parse 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 ] ] ] ] ] ] )
When the UTC function is called, the following steps are taken:
- 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 𝔽. - If y is
NaN , let yr beNaN . - Else,
- Let yi be !
ToIntegerOrInfinity (y). - If 0 ≤ yi ≤ 99, let yr be
1900 𝔽 + 𝔽(yi); otherwise, let yr be y.
- Let yi be !
- Return
TimeClip (MakeDate (MakeDay (yr, m, dt),MakeTime (h, min, s, milli))).
The UTC function is
The UTC 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
The abstract operation thisTimeValue takes argument value. It performs the following steps when called:
- If
Type (value) is Object and value has a [[DateValue]] internal slot, then- Return value.[[DateValue]].
- Throw a
TypeError exception.
In following descriptions of functions that are properties of the Date prototype object, the phrase “this Date object” refers to the object that is the
21.4.4.1 Date.prototype.constructor
The initial value of Date.prototype.constructor is
21.4.4.2 Date.prototype.getDate ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return DateFromTime(
LocalTime (t)).
21.4.4.3 Date.prototype.getDay ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return WeekDay(
LocalTime (t)).
21.4.4.4 Date.prototype.getFullYear ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
YearFromTime (LocalTime (t)).
21.4.4.5 Date.prototype.getHours ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
HourFromTime (LocalTime (t)).
21.4.4.6 Date.prototype.getMilliseconds ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
msFromTime (LocalTime (t)).
21.4.4.7 Date.prototype.getMinutes ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
MinFromTime (LocalTime (t)).
21.4.4.8 Date.prototype.getMonth ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
MonthFromTime (LocalTime (t)).
21.4.4.9 Date.prototype.getSeconds ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
SecFromTime (LocalTime (t)).
21.4.4.10 Date.prototype.getTime ( )
The following steps are performed:
- Return ?
thisTimeValue (this value).
21.4.4.11 Date.prototype.getTimezoneOffset ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return (t -
LocalTime (t)) /msPerMinute .
21.4.4.12 Date.prototype.getUTCDate ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return DateFromTime(t).
21.4.4.13 Date.prototype.getUTCDay ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return WeekDay(t).
21.4.4.14 Date.prototype.getUTCFullYear ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
YearFromTime (t).
21.4.4.15 Date.prototype.getUTCHours ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
HourFromTime (t).
21.4.4.16 Date.prototype.getUTCMilliseconds ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
msFromTime (t).
21.4.4.17 Date.prototype.getUTCMinutes ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
MinFromTime (t).
21.4.4.18 Date.prototype.getUTCMonth ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
MonthFromTime (t).
21.4.4.19 Date.prototype.getUTCSeconds ( )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , returnNaN . - Return
SecFromTime (t).
21.4.4.20 Date.prototype.setDate ( date )
The following steps are performed:
- Let t be
LocalTime (?thisTimeValue (this value)). - Let dt be ?
ToNumber (date). - Let newDate be
MakeDate (MakeDay (YearFromTime (t),MonthFromTime (t), dt),TimeWithinDay (t)). - Let u be
TimeClip (UTC (newDate)). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
21.4.4.21 Date.prototype.setFullYear ( year [ , month [ , date ] ] )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , set t to+0 𝔽; otherwise, set t toLocalTime (t). - Let y be ?
ToNumber (year). - If month is not present, let m be
MonthFromTime (t); otherwise, let m be ?ToNumber (month). - If date is not present, let dt be DateFromTime(t); otherwise, let dt be ?
ToNumber (date). - Let newDate be
MakeDate (MakeDay (y, m, dt),TimeWithinDay (t)). - Let u be
TimeClip (UTC (newDate)). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
The setFullYear method is
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 ] ] ] )
The following steps are performed:
- Let t be
LocalTime (?thisTimeValue (this value)). - Let h be ?
ToNumber (hour). - If min is not present, let m be
MinFromTime (t); otherwise, let m be ?ToNumber (min). - If sec is not present, let s be
SecFromTime (t); otherwise, let s be ?ToNumber (sec). - If ms is not present, let milli be
msFromTime (t); otherwise, let milli be ?ToNumber (ms). - Let date be
MakeDate (Day (t),MakeTime (h, m, s, milli)). - Let u be
TimeClip (UTC (date)). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
The setHours method is
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 )
The following steps are performed:
- Let t be
LocalTime (?thisTimeValue (this value)). Set ms to ?ToNumber (ms).- Let time be
MakeTime (HourFromTime (t),MinFromTime (t),SecFromTime (t), ms). - Let u be
TimeClip (UTC (MakeDate (Day (t), time))). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
21.4.4.24 Date.prototype.setMinutes ( min [ , sec [ , ms ] ] )
The following steps are performed:
- Let t be
LocalTime (?thisTimeValue (this value)). - Let m be ?
ToNumber (min). - If sec is not present, let s be
SecFromTime (t); otherwise, let s be ?ToNumber (sec). - If ms is not present, let milli be
msFromTime (t); otherwise, let milli be ?ToNumber (ms). - Let date be
MakeDate (Day (t),MakeTime (HourFromTime (t), m, s, milli)). - Let u be
TimeClip (UTC (date)). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
The setMinutes method is
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 ] )
The following steps are performed:
- Let t be
LocalTime (?thisTimeValue (this value)). - Let m be ?
ToNumber (month). - If date is not present, let dt be DateFromTime(t); otherwise, let dt be ?
ToNumber (date). - Let newDate be
MakeDate (MakeDay (YearFromTime (t), m, dt),TimeWithinDay (t)). - Let u be
TimeClip (UTC (newDate)). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
The setMonth method is
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 ] )
The following steps are performed:
- Let t be
LocalTime (?thisTimeValue (this value)). - Let s be ?
ToNumber (sec). - If ms is not present, let milli be
msFromTime (t); otherwise, let milli be ?ToNumber (ms). - Let date be
MakeDate (Day (t),MakeTime (HourFromTime (t),MinFromTime (t), s, milli)). - Let u be
TimeClip (UTC (date)). Set the [[DateValue]] internal slot ofthis Date object to u.- Return u.
The setSeconds method is
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 )
The following steps are performed:
- Perform ?
thisTimeValue (this value). - Let t be ?
ToNumber (time). - Let v be
TimeClip (t). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
21.4.4.28 Date.prototype.setUTCDate ( date )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - Let dt be ?
ToNumber (date). - Let newDate be
MakeDate (MakeDay (YearFromTime (t),MonthFromTime (t), dt),TimeWithinDay (t)). - Let v be
TimeClip (newDate). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
21.4.4.29 Date.prototype.setUTCFullYear ( year [ , month [ , date ] ] )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - If t is
NaN , set t to+0 𝔽. - Let y be ?
ToNumber (year). - If month is not present, let m be
MonthFromTime (t); otherwise, let m be ?ToNumber (month). - If date is not present, let dt be DateFromTime(t); otherwise, let dt be ?
ToNumber (date). - Let newDate be
MakeDate (MakeDay (y, m, dt),TimeWithinDay (t)). - Let v be
TimeClip (newDate). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
The setUTCFullYear method is
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 ] ] ] )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - Let h be ?
ToNumber (hour). - If min is not present, let m be
MinFromTime (t); otherwise, let m be ?ToNumber (min). - If sec is not present, let s be
SecFromTime (t); otherwise, let s be ?ToNumber (sec). - If ms is not present, let milli be
msFromTime (t); otherwise, let milli be ?ToNumber (ms). - Let newDate be
MakeDate (Day (t),MakeTime (h, m, s, milli)). - Let v be
TimeClip (newDate). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
The setUTCHours method is
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 )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - Let milli be ?
ToNumber (ms). - Let time be
MakeTime (HourFromTime (t),MinFromTime (t),SecFromTime (t), milli). - Let v be
TimeClip (MakeDate (Day (t), time)). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
21.4.4.32 Date.prototype.setUTCMinutes ( min [ , sec [ , ms ] ] )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - Let m be ?
ToNumber (min). - If sec is not present, let s be
SecFromTime (t). - Else,
- Let s be ?
ToNumber (sec).
- Let s be ?
- If ms is not present, let milli be
msFromTime (t). - Else,
- Let milli be ?
ToNumber (ms).
- Let milli be ?
- Let date be
MakeDate (Day (t),MakeTime (HourFromTime (t), m, s, milli)). - Let v be
TimeClip (date). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
The setUTCMinutes method is
If sec is not present, this method behaves as if sec was present with the value getUTCSeconds(). If ms is not present, it function behaves as if ms was present with the value return by getUTCMilliseconds().
21.4.4.33 Date.prototype.setUTCMonth ( month [ , date ] )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - Let m be ?
ToNumber (month). - If date is not present, let dt be DateFromTime(t).
- Else,
- Let dt be ?
ToNumber (date).
- Let dt be ?
- Let newDate be
MakeDate (MakeDay (YearFromTime (t), m, dt),TimeWithinDay (t)). - Let v be
TimeClip (newDate). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
The setUTCMonth method is
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 ] )
The following steps are performed:
- Let t be ?
thisTimeValue (this value). - Let s be ?
ToNumber (sec). - If ms is not present, let milli be
msFromTime (t). - Else,
- Let milli be ?
ToNumber (ms).
- Let milli be ?
- Let date be
MakeDate (Day (t),MakeTime (HourFromTime (t),MinFromTime (t), s, milli)). - Let v be
TimeClip (date). Set the [[DateValue]] internal slot ofthis Date object to v.- Return v.
The setUTCSeconds method is
If ms is not present, this method behaves as if ms was present with the value getUTCMilliseconds().
21.4.4.35 Date.prototype.toDateString ( )
The following steps are performed:
- Let O be
this Date object . - Let tv be ?
thisTimeValue (O). - If tv is
NaN , return"Invalid Date" . - Let t be
LocalTime (tv). - Return
DateString (t).
21.4.4.36 Date.prototype.toISOString ( )
If
21.4.4.37 Date.prototype.toJSON ( key )
This function provides a String representation of a Date object for use by JSON.stringify (
When the toJSON method is called with argument key, the following steps are taken:
- Let O be ?
ToObject (this value). - Let tv be ?
ToPrimitive (O,number ). - If
Type (tv) is Number and tv is not finite, returnnull . - Return ?
Invoke (O,"toISOString" ).
The argument is ignored.
The toJSON function 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 the Date.prototype.toLocaleDateString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleDateString method is used.
This function 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 the Date.prototype.toLocaleString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleString method is used.
This function 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 the Date.prototype.toLocaleTimeString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleTimeString method is used.
This function 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 ( )
The following steps are performed:
- Let tv be ?
thisTimeValue (this value). - Return
ToDateString (tv).
For any Date object d such that d.[[DateValue]] is evenly divisible by 1000, the result of Date.parse(d.toString()) = d.valueOf(). See
The toString function is not generic; it throws a
21.4.4.41.1 TimeString ( tv )
The abstract operation TimeString takes argument tv. It performs the following steps when called:
Assert :Type (tv) is Number.Assert : tv is notNaN .- Let hour be the String representation of
HourFromTime (tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary. - Let minute be the String representation of
MinFromTime (tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary. - Let second be the String representation of
SecFromTime (tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary. - 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. It performs the following steps when called:
Assert :Type (tv) is Number.Assert : tv is notNaN .- Let weekday be the Name of the entry in
Table 52 with the Number WeekDay(tv). - Let month be the Name of the entry in
Table 53 with the NumberMonthFromTime (tv). - Let day be the String representation of DateFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
- Let yv be
YearFromTime (tv). - If yv ≥
+0 𝔽, let yearSign be the empty String; otherwise, let yearSign be"-" . - Let year be the String representation of
abs (ℝ(yv)), formatted as a decimal number. - Let paddedYear be !
StringPad (year,4 𝔽,"0" ,start ). - 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. It performs the following steps when called:
Assert :Type (tv) is Number.Assert : tv is notNaN .- Let offset be
LocalTZA (tv,true ). - If 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 the String representation of
MinFromTime (absOffset), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary. - Let offsetHour be the String representation of
HourFromTime (absOffset), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary. - 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. It performs the following steps when called:
Assert :Type (tv) is Number.- If tv is
NaN , return"Invalid Date" . - Let t be
LocalTime (tv). - Return the
string-concatenation ofDateString (t), the code unit 0x0020 (SPACE),TimeString (t), andTimeZoneString (tv).
21.4.4.42 Date.prototype.toTimeString ( )
The following steps are performed:
- Let O be
this Date object . - Let tv be ?
thisTimeValue (O). - If tv is
NaN , return"Invalid Date" . - Let t be
LocalTime (tv). - Return the
string-concatenation ofTimeString (t) andTimeZoneString (tv).
21.4.4.43 Date.prototype.toUTCString ( )
The toUTCString method returns a String value representing the instance in time corresponding to
- Let O be
this Date object . - Let tv be ?
thisTimeValue (O). - If tv is
NaN , return"Invalid Date" . - Let weekday be the Name of the entry in
Table 52 with the Number WeekDay(tv). - Let month be the Name of the entry in
Table 53 with the NumberMonthFromTime (tv). - Let day be the String representation of DateFromTime(tv), formatted as a two-digit decimal number, padded to the left with the code unit 0x0030 (DIGIT ZERO) if necessary.
- Let yv be
YearFromTime (tv). - If yv ≥
+0 𝔽, let yearSign be the empty String; otherwise, let yearSign be"-" . - Let year be the String representation of
abs (ℝ(yv)), formatted as a decimal number. - Let paddedYear be !
StringPad (year,4 𝔽,"0" ,start ). - 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 ( )
The following steps are performed:
- Return ?
thisTimeValue (this value).
21.4.4.45 Date.prototype [ @@toPrimitive ] ( hint )
This function is called by ECMAScript language operators to convert a Date object to a primitive value. The allowed values for hint are
When the @@toPrimitive method is called with argument hint, the following steps are taken:
- Let O be the
this value. - If
Type (O) is not Object, throw aTypeError exception. - If hint is
"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. - Return ?
OrdinaryToPrimitive (O, tryFirst).
The value of the
This property has the attributes { [[Writable]]:
21.4.5 Properties of Date Instances
Date instances are ordinary objects that inherit properties from the