The Number constructor is the %Number% intrinsic object and the initial value of the Number
property of
the global object. When called as a constructor, it creates and initializes a new Number object. When Number
is called as a function rather than as a constructor, it performs a type conversion.
The Number
constructor is designed to be subclassable. It may be used as the value of an
extends
clause of a class definition. Subclass constructors that intend to inherit the specified
Number
behaviour must include a super
call to the Number
constructor to create and
initialize the subclass instance with a [[NumberData]] internal
slot.
When Number
is called with argument number, the following steps are
taken:
"%NumberPrototype%"
, «[[NumberData]]» ).The value of the [[Prototype]] internal slot of the Number constructor is the intrinsic object %FunctionPrototype% (19.2.3).
Besides the length
property (whose value is 1), the Number constructor has the following
properties:
The value of Number.EPSILON is the difference between 1 and the smallest value greater than 1 that is representable as a Number value, which is approximately 2.2204460492503130808472633361816 x 10−16.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
When the Number.isFinite
is called with one argument number, the following
steps are taken:
When the Number.isInteger
is called with one argument number, the
following steps are taken:
When the Number.isNaN
is called with one argument number, the following
steps are taken:
NOTE This function differs from the global isNaN function (18.2.3) is that it does not convert its argument to a Number before determining whether it is NaN.
When the Number.isSafeInteger
is called with one argument number, the
following steps are taken:
NOTE The value of Number.MAX_SAFE_INTEGER
is the largest integer n such that n
and n + 1 are both exactly representable as a Number value.
The value of Number.MAX_SAFE_INTEGER is 9007199254740991 (253−1).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]:
false }.
The value of Number.MAX_VALUE
is the largest positive finite value of the Number type, which is
approximately 1.7976931348623157 × 10308.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
NOTE The value of Number.MIN_SAFE_INTEGER
is the smallest integer n such that
n and n − 1 are both exactly representable as a Number value.
The value of Number.MIN_SAFE_INTEGER is −9007199254740991 (−(253−1)).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The value of Number.MIN_VALUE
is the smallest positive value of the Number type, which is approximately
5 × 10−324.
In the IEEE 754-2008 double precision binary representation, the smallest possible value is a denormalized number. If
an implementation does not support denormalized values, the value of 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]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The value of Number.NaN
is NaN.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The value of Number.NEGATIVE_INFINITY is −∞.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The value of the Number.parseFloat
data property is the same built-in function object that is the value of
the parseFloat
property of the global object defined in 18.2.4.
The value of the Number.parseInt
data property is the same built-in function object that is the value of
the parseInt
property of the global object defined in 18.2.5.
The value of Number.POSITIVE_INFINITY is +∞.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The initial value of Number.prototype
is the intrinsic object %NumberPrototype% (20.1.3).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The Number prototype object is the intrinsic object %NumberPrototype%. The Number prototype object is an ordinary object. It is not a Number instance and does not have a [[NumberData]] internal slot.
The value of the [[Prototype]] internal slot of the Number prototype object is the intrinsic object %ObjectPrototype% (19.1.3).
Unless explicitly stated otherwise, the methods of the Number prototype object defined below are not generic and the this value passed to them must be either a Number value or an object that has a [[NumberData]] internal slot that has been initialized to a Number value.
The abstract operation thisNumberValue(value) performs the following steps:
The phrase “this Number value” within the specification of a method refers to the result returned by calling the abstract operation thisNumberValue with the this value of the method invocation passed as the argument.
The initial value of Number.prototype.constructor
is the intrinsic object %Number%.
Return 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 undefined, include as many significand digits as necessary to uniquely specify the Number (just like in ToString except that in this case the Number is always output in exponential notation). Specifically, perform the following steps:
"NaN"
."-"
."Infinity"
.toExponential
for values of f less than 0 or greater than 20. In this
case toExponential
would not necessarily throw RangeError for such values."."
, and b."+".
"0".
"+".
"-"
."e"
, c, and d.The length
property of the toExponential
method is 1.
If the toExponential
method is called with more than one argument, then the behaviour is undefined (see clause 17).
NOTE For implementations that provide more accurate conversions than required by the rules above, it is recommended that the following alternative version of step 12.b.i be used as a guideline:
i. Let e, n, and f be integers such that f ≥ 0, 10f ≤ n < 10f+1, the Number value for 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.
NOTE 1 toFixed
returns a String containing this Number value represented in
decimal fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits
is undefined, 0 is assumed.
The following steps are performed:
0
).toFixed
for values of f less than 0 or greater than 20. In this case
toFixed
would not necessarily throw RangeError for such values."NaN"
.-
"."0"
. Otherwise, let m be the String consisting
of the digits of the decimal representation of n (in order, with no leading zeroes)."."
, and b.The length
property of the toFixed
method is 1.
If the toFixed
method is called with more than one argument, then the behaviour is undefined (see clause 17).
NOTE 2 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 "1000000000000000100"
,
while(1000000000000000128).toFixed(0
) returns "1000000000000000128"
.
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 Number value formatted according to the conventions of the host
environment's current locale. This function is implementation-dependent, and it is permissible, but not encouraged,
for it to return the same thing as 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.
The length
property of the toLocaleString
method is 0.
Return a String containing this Number value represented either in decimal exponential notation with one digit before the significand's decimal point and precision–1 digits after the significand's decimal point or in decimal fixed notation with precision significant digits. If precision is undefined, call ToString (7.1.12) instead. Specifically, perform the following steps:
"NaN"
."Infinity"
.toPrecision
for values of p less than 1 or greater than 21. In this
case toPrecision
would not necessarily throw RangeError for such values.The length
property of the toPrecision
method is 1.
If the toPrecision
method is called with more than one argument, then the behaviour is undefined (see clause 17).
NOTE The optional radix should be an integer value in the inclusive range 2 to 36. If radix not present or is undefined the Number 10 is used as the value of radix.
The following steps are performed:
a
-z
are used for digits with values 10 through 35. The precise algorithm is
implementation-dependent, however the algorithm should be a generalization of that specified in 7.1.12.1.The toString
function is not generic; it throws a TypeError exception if its this value is
not a Number or a Number object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
Number instances are ordinary objects that inherit properties from the Number prototype object. Number instances also have a [[NumberData]] internal slot. The [[NumberData]] internal slot is the Number value represented by this Number object.
The Math object is the %Math% intrinsic object and the initial value of the Math
property of the global
object. The Math object is a single ordinary object.
The value of the [[Prototype]] internal slot of the Math object is the intrinsic object %ObjectPrototype% (19.1.3).
The Math object is not a function object. It does not have a [[Construct]] internal method; it is not possible to use the
Math object as a constructor with the new
operator. The Math object also does not have a [[Call]] internal
method; it is not possible to invoke the Math object as a function.
NOTE In this specification, the phrase “the Number value for x” has a technical meaning defined in 6.1.6.
The Number value for e, the base of the natural logarithms, which is approximately 2.7182818284590452354.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The Number value for the natural logarithm of 10, which is approximately 2.302585092994046.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The Number value for the natural logarithm of 2, which is approximately 0.6931471805599453.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The Number value for the base-10 logarithm of e, the base of the natural logarithms; this value is approximately 0.4342944819032518.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
NOTE The value of Math.LOG10E
is approximately the reciprocal of the value of
Math.LN10
.
The Number value for the base-2 logarithm of e, the base of the natural logarithms; this value is approximately 1.4426950408889634.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
NOTE The value of Math.LOG2E
is approximately the reciprocal of the value of
Math.LN2
.
The Number value for π, the ratio of the circumference of a circle to its diameter, which is approximately 3.1415926535897932.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The Number value for the square root of ½, which is approximately 0.7071067811865476.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
NOTE The value of Math.SQRT1_2
is approximately the reciprocal of the value of
Math.SQRT2
.
The Number value for the square root of 2, which is approximately 1.4142135623730951.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The initial value of the @@toStringTag property is the String value "Math"
.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.
Each of the following Math
object functions applies the ToNumber abstract
operation to each of its arguments (in left-to-right order if there is more than one). If ToNumber returns an abrupt completion,
that Completion Record is immediately returned. Otherwise, the
function performs a computation on the resulting Number value(s). The value returned by each function is a Number.
In the function descriptions below, the symbols NaN, −0, +0, −∞ and +∞ refer to the Number values described in 6.1.6.
NOTE 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 IEEE 754-2008 arithmetic contained in
fdlibm
, the freely distributable mathematical library from Sun Microsystems (http://www.netlib.org/fdlibm)
.
Returns the absolute value of x; the result has the same magnitude as x but has positive sign.
Returns an implementation-dependent approximation to the arc cosine of x. The result is expressed in radians and ranges from +0 to +π.
Returns an implementation-dependent approximation to the inverse hyperbolic cosine of x.
Returns an implementation-dependent approximation to the arc sine of x. The result is expressed in radians and ranges from −π/2 to +π/2.
Returns an implementation-dependent approximation to the inverse hyperbolic sine of x.
Returns an implementation-dependent approximation to the arc tangent of x. The result is expressed in radians and ranges from −π/2 to +π/2.
Returns an implementation-dependent approximation to the inverse hyperbolic tangent of x.
Returns an implementation-dependent approximation to the arc tangent of the quotient y/x of the arguments y and x, where the signs of y and x are used to determine the quadrant of the result. Note that it is intentional and traditional for the two-argument arc tangent function that the argument named y be first and the argument named x be second. The result is expressed in radians and ranges from −π to +π.
Returns an implementation-dependent approximation to the cube root of x.
Returns the smallest (closest to −∞) Number value that is not less than x and is equal to a mathematical integer. If x is already an integer, the result is x.
The value of Math.ceil(x)
is the same as the value of -Math.floor(-x)
.
When Math.clz32
is called with one argument x, the following steps are
taken:
NOTE If n is 0, p will be 32. If the most significant bit of the 32-bit binary encoding of n is 1, p will be 0.
Returns an implementation-dependent approximation to the cosine of x. The argument is expressed in radians.
Returns an implementation-dependent approximation to the hyperbolic cosine of x.
NOTE The value of cosh(x) is the same as (exp(x) + exp(-x))/2.
Returns an implementation-dependent approximation to the exponential function of x (e raised to the power of x, where e is the base of the natural logarithms).
Returns an implementation-dependent approximation to 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 0.
Returns the greatest (closest to +∞) Number value that is not greater than x and is equal to a mathematical integer. If x is already an integer, the result is x.
When Math.fround
is called with argument x the following steps are taken:
Math.hypot
returns an implementation-dependent approximation of the square root of
the sum of squares of its arguments.
The length property of the hypot
function is 2.
NOTE 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.
When the Math.imul
is called with arguments x and y the
following steps are taken:
Returns an implementation-dependent approximation to the natural logarithm of x.
Returns an implementation-dependent approximation to 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.
Returns an implementation-dependent approximation to the base 10 logarithm of x.
Returns an implementation-dependent approximation to the base 2 logarithm of x.
Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.
If no arguments are given, the result is −∞.
If any value is NaN, the result is NaN.
The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0.
The length
property of the max
method is 2.
Given zero or more arguments, calls ToNumber on each of the arguments and returns the smallest of the resulting values.
If no arguments are given, the result is +∞.
If any value is NaN, the result is NaN.
The comparison of values to determine the smallest value is done using the Abstract Relational Comparison algorithm (7.2.11) except that +0 is considered to be larger than −0.
The length
property of the min
method is 2.
Returns an implementation-dependent approximation to the result of raising x to the power y.
Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.
Each Math.random
function created for distinct code Realms must produce a distinct sequence of values from
successive calls.
Returns the Number value that is closest to x and is equal to a mathematical integer. If two integer Number values are equally close to x, then the result is the Number value that is closer to +∞. If x is already an integer, the result is x.
NOTE 1 Math.round(3.5)
returns 4, but Math.round(–3.5)
returns –3.
NOTE 2 The value of Math.round(x)
is not always the same as the value of
Math.floor(x+0.5)
. When x
is −0 or is less than 0 but greater than or equal to
-0.5, Math.round(x)
returns −0, but Math.floor(x+0.5)
returns +0. Math.round(x)
may also differ from the value of Math.floor(x+0.5)
because of internal rounding when computing x+0.5
.
Returns the sign of the x, indicating whether x is positive, negative or zero.
Returns an implementation-dependent approximation to the sine of x. The argument is expressed in radians.
Returns an implementation-dependent approximation to the hyperbolic sine of x.
NOTE The value of sinh(x) is the same as (exp(x) - exp(-x))/2.
Returns an implementation-dependent approximation to the square root of x.
Returns an implementation-dependent approximation to the tangent of x. The argument is expressed in radians.
Returns an implementation-dependent approximation to the hyperbolic tangent of x.
NOTE The value of tanh(x) is the same as (exp(x) - exp(-x))/(exp(x) + exp(-x)).
Returns the integral part of the number x, removing any fractional digits. If x is already an integer, the result is x.
The following functions are abstract operations that operate on time values (defined in 20.3.1.1). Note that, in every case, if any argument to one of these functions is NaN, the result will be NaN.
A Date object contains a Number indicating a particular instant in time to within a millisecond. Such a Number is called a time value. A time value may also be NaN, indicating that the Date object does not represent a specific instant of time.
Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. In time values leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript Number values can represent all integers from –9,007,199,254,740,992 to 9,007,199,254,740,992; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years, either forward or backward, from 01 January, 1970 UTC.
The actual range of times supported by ECMAScript Date objects is slightly smaller: exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC.
The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0.
A given time value t belongs to day number
where the number of milliseconds per day is
The remainder is called the time within the day:
ECMAScript uses an extrapolated Gregorian system to map a day number to a year number and to determine the month and date within that year. In this system, 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
DaysInYear(y)
= 365 if (y modulo 4)
≠ 0
= 366 if (y modulo 4) = 0 and (y modulo 100) ≠ 0
= 365 if (y modulo 100) = 0 and (y modulo 400)
≠ 0
= 366 if (y modulo 400) = 0
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:
DayFromYear(y) = 365 × (y−1970) + floor((y−1969)/4) − floor((y−1901)/100) + floor((y−1601)/400)
The time value of the start of a year is:
TimeFromYear(y) = msPerDay × DayFromYear(y)
A time value determines a year by:
YearFromTime(t) = the largest integer y (closest to positive infinity) such that TimeFromYear(y) ≤ t
The leap-year function is 1 for a time within a leap year and otherwise is zero:
InLeapYear(t)
= 0 if DaysInYear(YearFromTime(t)) = 365
= 1 if
DaysInYear(YearFromTime(t)) = 366
Months are identified by an integer in the range 0 to 11, inclusive. The mapping MonthFromTime(t) from a time value t to a month number is defined by:
MonthFromTime(t)
= 0 if 0 ≤ DayWithinYear(t) <
31
= 1 if 31 ≤ DayWithinYear (t) < 59+InLeapYear(t)
= 2 if 59+InLeapYear(t)
≤ DayWithinYear (t) < 90+InLeapYear(t)
= 3 if 90+InLeapYear(t) ≤ DayWithinYear (t) < 120+InLeapYear(t)
= 4 if 120+InLeapYear(t) ≤ DayWithinYear (t) < 151+InLeapYear(t)
= 5 if 151+InLeapYear(t) ≤ DayWithinYear (t) < 181+InLeapYear(t)
= 6 if 181+InLeapYear(t) ≤ DayWithinYear (t) < 212+InLeapYear(t)
= 7 if 212+InLeapYear(t) ≤ DayWithinYear (t) < 243+InLeapYear(t)
= 8 if 243+InLeapYear(t) ≤ DayWithinYear (t) < 273+InLeapYear(t)
= 9 if 273+InLeapYear(t) ≤ DayWithinYear (t) < 304+InLeapYear(t)
= 10 if 304+InLeapYear(t) ≤ DayWithinYear (t) < 334+InLeapYear(t)
= 11 if 334+InLeapYear(t) ≤ DayWithinYear (t) < 365+InLeapYear(t)
where
DayWithinYear(t) = Day(t)−DayFromYear(YearFromTime(t))
A month value of 0 specifies January; 1 specifies February; 2 specifies March; 3 specifies April; 4 specifies May; 5 specifies June; 6 specifies July; 7 specifies August; 8 specifies September; 9 specifies October; 10 specifies November; and 11 specifies December. Note that MonthFromTime(0) = 0, corresponding to Thursday, 01 January, 1970.
A date number is identified by an integer in the range 1 through 31, inclusive. The mapping DateFromTime(t) from a time value t to a date number is defined by:
DateFromTime(t)
= DayWithinYear(t)+1 if MonthFromTime(t)=0
= DayWithinYear(t)−30 if MonthFromTime(t)=1
= DayWithinYear(t)−58−InLeapYear(t) if MonthFromTime(t)=2
= DayWithinYear(t)−89−InLeapYear(t) if MonthFromTime(t)=3
= DayWithinYear(t)−119−InLeapYear(t) if MonthFromTime(t)=4
= DayWithinYear(t)−150−InLeapYear(t) if MonthFromTime(t)=5
= DayWithinYear(t)−180−InLeapYear(t) if MonthFromTime(t)=6
= DayWithinYear(t)−211−InLeapYear(t) if MonthFromTime(t)=7
= DayWithinYear(t)−242−InLeapYear(t) if MonthFromTime(t)=8
= DayWithinYear(t)−272−InLeapYear(t) if MonthFromTime(t)=9
= DayWithinYear(t)−303−InLeapYear(t) if MonthFromTime(t)=10
= DayWithinYear(t)−333−InLeapYear(t) if MonthFromTime(t)=11
The weekday for a particular time value t is defined as
WeekDay(t) = (Day(t) + 4) modulo 7
A weekday value of 0 specifies Sunday; 1 specifies Monday; 2 specifies Tuesday; 3 specifies Wednesday; 4 specifies Thursday; 5 specifies Friday; and 6 specifies Saturday. Note that WeekDay(0) = 4, corresponding to Thursday, 01 January, 1970.
An implementation of ECMAScript is expected to determine the local time zone adjustment. The local time zone adjustment is a value LocalTZA measured in milliseconds which when added to UTC represents the local standard time. Daylight saving time is not reflected by LocalTZA.
NOTE It is recommended that implementations use the time zone information of the IANA Time Zone Database http://www.iana.org/time-zones/.
An implementation dependent algorithm using best available information on time zones to determine the local daylight saving time adjustment DaylightSavingTA(t), measured in milliseconds. An implementation of ECMAScript is expected to make its best effort to determine the local daylight saving time adjustment.
NOTE It is recommended that implementations use the time zone information of the IANA Time Zone Database http://www.iana.org/time-zones/.
The abstract operation LocalTime with argument t converts t from UTC to local time by performing the following steps:
The abstract operation UTC with argument t converts t from local time to UTC is defined by performing the following steps:
NOTE UTC(LocalTime(t)) is not necessarily always equal to t.
The following abstract operations are useful in decomposing time values:
HourFromTime(t) = floor(t / msPerHour) modulo HoursPerDay
MinFromTime(t) = floor(t / msPerMinute) modulo MinutesPerHour
SecFromTime(t) = floor(t / msPerSecond) modulo SecondsPerMinute
msFromTime(t) = t modulo msPerSecond
where
HoursPerDay = 24
MinutesPerHour = 60
SecondsPerMinute = 60
msPerSecond = 1000
msPerMinute = 60000 = msPerSecond × SecondsPerMinute
msPerHour = 3600000 = msPerMinute × MinutesPerHour
The abstract operation MakeTime calculates a number of milliseconds from its four arguments, which must be ECMAScript Number values. This operator functions as follows:
*
msPerHour
+
m *
msPerMinute
+
s *
msPerSecond
+
milli, performing the arithmetic according to IEEE 754-2008 rules (that is, as if using the
ECMAScript operators *
and +
).The abstract operation MakeDay calculates a number of days from its three arguments, which must be ECMAScript Number values. This operator functions as follows:
The abstract operation MakeDate calculates a number of milliseconds from its two arguments, which must be ECMAScript Number values. This operator functions as follows:
The abstract operation TimeClip calculates a number of milliseconds from its argument, which must be an ECMAScript Number value. This operator functions as follows:
NOTE The point of step 3 is that an implementation is permitted a choice of internal representations of time values, for example as a 64-bit signed integer or as a 64-bit floating-point value. Depending on the implementation, this internal representation may or may not distinguish −0 and +0.
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended
Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Where the fields are as follows:
YYYY |
is the decimal digits of the year 0000 to 9999 in the Gregorian calendar. |
- |
"-" (hyphen) appears literally twice in the string. |
MM |
is the month of the year from 01 (January) to 12 (December). |
DD |
is the day of the month from 01 to 31. |
T |
"T" appears literally in the string, to indicate the beginning of the time element. |
HH |
is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24. |
: |
":" (colon) appears literally twice in the string. |
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. |
. |
"." (dot) appears literally in the string. |
sss |
is the number of complete milliseconds since the start of the second as three decimal digits. |
Z |
is the time zone offset specified as "Z" (for UTC) or either "+" or "-" followed by a time expression HH:mm |
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 time zone offset appended:
THH:mm
THH:mm:ss
THH:mm:ss.sss
All numbers must be base 10. If the MM
or
DD
fields are absent "01"
is used as the value. If the HH
, mm
, or
ss
fields are absent "00"
is used as the value and the value of an absent sss
field is "000"
. If the time zone offset is absent, the date-time is interpreted as a local time.
Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format.
NOTE 1 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
NOTE 2 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, ISO 8601 and this format specifies numeric representations of date and time.
ECMAScript requires the ability to specify 6 digit years (extended years); approximately 285,426 years, either forward or backward, from 01 January, 1970 UTC. To represent years before 0 or after 9999, ISO 8601 permits the expansion of the year representation, but only by prior agreement between the sender and the receiver. In the simplified ECMAScript format such an expanded year representation shall have 2 extra year digits and is always prefixed with a + or – sign. The year 0 is considered positive and hence prefixed with a + sign.
NOTE Examples of extended years:
-283457-03-21T15:00:59.008Z |
283458 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. |
+287396-10-12T08:59:00.992Z |
287396 A.D. |
The Date constructor is the %Date% intrinsic object and the initial value of the Date
property of the
global object. When called as a constructor it creates and initializes a new Date object. When Date
is called
as a function rather than as a constructor, it returns a String representing the current time (UTC).
The Date
constructor is a single function whose behaviour is overloaded based upon the number and types of
its arguments.
The Date
constructor is designed to be subclassable. It may be used as the value of an
extends
clause of a class definition. Subclass constructors that intend to inherit the specified
Date
behaviour must include a super
call to the Date
constructor to create and
initialize the subclass instance with a [[DateValue]] internal
slot.
This description applies only if the Date constructor is called with at least two arguments.
When the Date
function is called the following steps are taken:
"%DatePrototype%"
, « [[DateValue]]»).This description applies only if the Date constructor is called with exactly one argument.
When the Date
function is called the following steps are taken:
parse
method (20.3.3.2). If the parse resulted in an abrupt completion, tv is the Completion Record."%DatePrototype%"
, « [[DateValue]]»).This description applies only if the Date constructor is called with no arguments.
When the Date
function is called the following steps are taken:
"%DatePrototype%"
, « [[DateValue]]»).The value of the [[Prototype]] internal slot of the Date constructor is the intrinsic object %FunctionPrototype% (19.2.3).
Besides the length
property (whose value is 7
), the Date constructor has the following
properties:
The now
function returns a Number value that is the time
value designating the UTC date and time of the occurrence of the call to now
.
The parse
function applies the ToString operator to its argument. If ToString results in an abrupt completion
the Completion Record is immediately returned. Otherwise,
parse
interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as
a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first
attempts to parse the format of the String according to the rules (including extended years) called out in Date Time
String Format (20.3.1.16). If the String does not conform to that format the
function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable
Strings or dates containing illegal element values in the format String shall cause Date.parse
to return
NaN.
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 Number value as the preceding three expressions and, in general, the value produced
by Date.parse
is implementation-dependent when given any String value that does not conform to the Date Time
String Format (20.3.1.16) and that could not be produced in that implementation
by the toString
or toUTCString
method.
The initial value of Date.prototype
is the intrinsic object %DatePrototype% (20.3.4).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
When the UTC
function is called with fewer than two arguments, the behaviour is
implementation-dependent. When the UTC
function is called with two to seven arguments, it computes the date
from year, month and (optionally) date, hours, minutes,
seconds and ms. The following steps are taken:
The length
property of the UTC
function is 7.
NOTE The UTC
function differs from the Date
constructor in two
ways: it returns a time value as a Number, rather than creating a Date
object, and it interprets the arguments in UTC rather than as local time.
The Date prototype object is the intrinsic object %DatePrototype%. The Date prototype object is itself an ordinary object. It is not a Date instance and does not have a [[DateValue]] internal slot.
The value of the [[Prototype]] internal slot of the Date prototype object is the intrinsic object %ObjectPrototype% (20.3.4).
Unless explicitly defined otherwise, the methods of the Date prototype object defined below are not generic and the this value passed to them must be an object that has a [[DateValue]] internal slot that has been initialized to a time value.
The abstract operation thisTimeValue(value) performs the following steps:
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 this value for the invocation of the function. If the Type of the this value is not Object, a TypeError exception is thrown. The phrase “this time value” within the specification of a method refers to the result returned by calling the abstract operation thisTimeValue with the this value of the method invocation passed as the argument.
The initial value of Date.prototype.constructor
is the intrinsic object %Date%.
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The following steps are performed:
The length
property of the setFullYear
method is 3.
NOTE If month is not specified, this method behaves as if month were
specified with the value getMonth()
. If date is not specified, it behaves as if date
were specified with the value getDate()
.
The following steps are performed:
The length
property of the setHours
method is 4.
NOTE If min is not specified, this method behaves as if min were
specified with the value getMinutes()
. If sec is not specified, it behaves as if sec
were specified with the value getSeconds()
. If ms is not specified, it behaves as if
ms were specified with the value getMilliseconds()
.
The following steps are performed:
The following steps are performed:
The length
property of the setMinutes
method is 3.
NOTE If sec is not specified, this method behaves as if sec were
specified with the value getSeconds()
. If ms is not specified, this behaves as if ms
were specified with the value getMilliseconds()
.
The following steps are performed:
The length
property of the setMonth
method is 2.
NOTE If date is not specified, this method behaves as if date were
specified with the value getDate()
.
The following steps are performed:
The length
property of the setSeconds
method is 2.
NOTE If ms is not specified, this method behaves as if ms were
specified with the value getMilliseconds()
.
The following steps are performed:
The following steps are performed:
The length
property of the setUTCFullYear
method is 3.
NOTE If month is not specified, this method behaves as if month were
specified with the value getUTCMonth()
. If date is not specified, it behaves as if
date were specified with the value getUTCDate()
.
The following steps are performed:
The length
property of the setUTCHours
method is 4.
NOTE If min is not specified, this method behaves as if min were
specified with the value getUTCMinutes()
. If sec is not specified, it behaves as if
sec were specified with the value getUTCSeconds()
. If ms is not specified, it behaves
as if ms were specified with the value getUTCMilliseconds()
.
The following steps are performed:
The following steps are performed:
The length
property of the setUTCMinutes
method is 3.
NOTE If sec is not specified, this method behaves as if sec were
specified with the value getUTCSeconds()
. If ms is not specified, it function behaves as if
ms were specified with the value return by getUTCMilliseconds()
.
The following steps are performed:
The length
property of the setUTCMonth
method is 2.
NOTE If date is not specified, this method behaves as if date were
specified with the value getUTCDate()
.
The following steps are performed:
The length
property of the setUTCSeconds
method is 2.
NOTE If ms is not specified, this method behaves as if ms were
specified with the value getUTCMilliseconds()
.
This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the “date” portion of the Date in the current time zone in a convenient, human-readable form.
This function returns a String value representing the instance in time corresponding to this time value. The format of the String is the Date Time string format defined in 20.3.1.16. All fields are present in the String. The time zone is always UTC, denoted by the suffix Z. If this time value is not a finite Number or if the year is not a value that can be represented in that format (if necessary using extended year format), a RangeError exception is thrown.
This function provides a String representation of a Date object for use by JSON.stringify
(24.3.2).
When the toJSON
method is called with argument key, the following steps
are taken:
"toISOString"
).NOTE 1 The argument is ignored.
NOTE 2 The toJSON
function is intentionally generic; it does not require that
its this value be a Date object. Therefore, it can be transferred to other kinds of objects for use as a method.
However, it does require that any such object have a toISOString
method.
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 implementation-dependent, but are intended to represent the “date” portion of the Date in the current time zone in a convenient, human-readable form that corresponds to the conventions of the host environment's current locale.
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.
The length
property of the toLocaleDateString
method is 0.
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 implementation-dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form that corresponds to the conventions of the host environment's current locale.
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.
The length
property of the toLocaleString
method is 0.
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 implementation-dependent, but are intended to represent the “time” portion of the Date in the current time zone in a convenient, human-readable form that corresponds to the conventions of the host environment's current locale.
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.
The length
property of the toLocaleTimeString
method is 0.
The following steps are performed:
NOTE 1 For any Date object d whose
milliseconds amount is zero, the result of Date.parse(d.toString())
is equal to d.valueOf()
. See 20.3.3.2.
NOTE 2 The toString
function is intentionally generic; it does not require
that its this value be a Date object. Therefore, it can be transferred to other kinds of objects for use as a
method.
The following steps are performed:
This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the “time” portion of the Date in the current time zone in a convenient, human-readable form.
This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent this time value in a convenient, human-readable form in UTC.
NOTE The intent is to produce a String representation of a date that is more readable than
the format specified in 20.3.1.16. It is not essential that the chosen format
be unambiguous or easily machine parsable. If an implementation does not have a preferred human-readable format it is
recommended to use the format defined in 20.3.1.16 but with a space rather
than a "T"
used to separate the date and time elements.
The valueOf
function returns a Number, which is this time value.
This function is called by ECMAScript language operators to convert a Date object to a primitive value. The allowed
values for hint are "default"
, "number"
, and "string"
. Date objects,
are unique among built-in ECMAScript object in that they treat "default"
as being equivalent to
"string"
, All other built-in ECMAScript objects treat "default"
as being equivalent to
"number"
.
When the @@toPrimitive
method is called with argument hint, the following
steps are taken:
string
" or the String value "default
" , then
string
".number
", then
number
".The value of the name
property of this function is "[Symbol.toPrimitive]"
.
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }.
Date instances are ordinary objects that inherit properties from the Date prototype object. Date instances also have a [[DateValue]] internal slot. The [[DateValue]] internal slot is the time value represented by this Date object.