The following functions are abstract operations that operate on time values (defined in 15.9.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
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 time value of the start of a year is:
A time value determines a year by:
The leap-year function is 1 for a time within a leap year and otherwise is zero:
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:
where
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 month number is defined by:
The weekday for a particular time value t is defined as
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. The value LocalTZA does not vary with time but depends only on the geographic location.
An implementation of ECMAScript is expected to determine the daylight saving time algorithm. The algorithm to determine the daylight saving time adjustment DaylightSavingTA(t), measured in milliseconds, must depend only on four things:
(1) the time since the beginning of the year
(2) whether t is in a leap year
(3) the week day of the beginning of the year
and (4) the geographic location.
The implementation of ECMAScript should not try to determine whether the exact time was subject to daylight saving time, but just whether daylight saving time would have been in effect if the current daylight saving time algorithm had been used at the time. This avoids complications such as taking into account the years that the locale observed daylight saving time year round.
If the host environment provides functionality for determining daylight saving time, the implementation of ECMAScript is free to map the year in question to an equivalent year (same leap-year-ness and same starting week day for the year) for which the host environment provides daylight saving time information. The only restriction is that all equivalent years should produce the same result.
Conversion from UTC to local time is defined by
Conversion from local time to UTC is defined by
Note that UTC(LocalTime(t)) is not necessarily always equal to t.
The following functions are useful in decomposing time values:
where
The operator 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
rules (that is, as if using the ECMAScript operators *
and +
).The operator MakeDay calculates a number of days from its three arguments, which must be ECMAScript Number values. This operator functions as follows:
==
ym and MonthFromTime(t) ==
mn and DateFromTime(t) ==
1; but if this is not possible (because some
argument is out of range), return NaN.The operator MakeDate calculates a number of milliseconds from its two arguments, which must be ECMAScript Number values. This operator functions as follows:
The operator 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
”. The value of an absent time zone offset is
“Z
”.
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.
When Date
is called as a function rather than as a constructor, it returns a String representing the current
time (UTC).
NOTE The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments.
All of the arguments are optional; any arguments supplied are accepted but are completely ignored. A String is created
and returned as if by the expression (new Date()).toString()
where Date
is the standard built-in
constructor with that name and toString
is the standard built-in method
Date.prototype.toString
.
When Date
is called as part of a new
expression, it is a constructor: it initialises the newly
created object.
When Date is called with two to seven arguments, it computes the date from year, month, and (optionally) date, hours, minutes, seconds and ms.
The [[Prototype]] internal property of the newly constructed object is set to the original Date prototype object, the
one that is the initial value of Date.prototype
(15.9.4.1).
The [[Class]] internal property of the newly constructed object is set to "Date"
.
The [[Extensible]] internal property of the newly constructed object is set to true.
The [[PrimitiveValue]] internal property of the newly constructed object is set as follows:
The [[Prototype]] internal property of the newly constructed object is set to the original Date prototype object, the
one that is the initial value of Date.prototype
(15.9.4.1).
The [[Class]] internal property of the newly constructed object is set to "Date"
.
The [[Extensible]] internal property of the newly constructed object is set to true.
The [[PrimitiveValue]] internal property of the newly constructed object is set as follows:
parse
method (15.9.4.2); let V be the time value for this
date.The [[Prototype]] internal property of the newly constructed object is set to the original Date prototype object, the
one that is the initial value of Date.prototype
(15.9.4.1).
The [[Class]] internal property of the newly constructed object is set to "Date"
.
The [[Extensible]] internal property of the newly constructed object is set to true.
The [[PrimitiveValue]] internal property of the newly constructed object is set to the time value (UTC) identifying the current time.
The value of the [[Prototype]] internal property of the Date constructor is the Function prototype object (15.3.4).
Besides the internal properties and the length
property (whose value is 7
), the Date
constructor has the following properties:
The initial value of Date.prototype
is the built-in Date prototype object (15.9.5).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
The parse
function applies the ToString operator to its argument and 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 called
out in Date Time String Format (15.9.1.15). If the String does not conform to that format the
function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognisable
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 (15.9.1.15) and that could not be produced in that implementation by the
toString
or toUTCString
method.
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 now
function return a Number value that is the time value designating the
UTC date and time of the occurrence of the call to now
.
The Date prototype object is itself a Date object (its [[Class]] is "Date"
) whose [[PrimitiveValue]] is
NaN.
The value of the [[Prototype]] internal property of the Date prototype object is the standard built-in Object prototype object (15.2.4).
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. Unless explicitly noted
otherwise, none of these functions are generic; a TypeError exception is thrown if the this value is not an
object for which the value of the [[Class]] internal property is "Date"
. Also, the phrase “this time value” refers to the Number value for the time represented by this Date object, that is,
the value of the [[PrimitiveValue]] internal property of this Date object.
The initial value of Date.prototype.constructor
is the built-in Date
constructor.
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.
NOTE For any Date value d whose milliseconds amount is zero, the result of Date.parse(d.toString()) is equal to d.valueOf(). See 15.9.4.2.
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. 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 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.
NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.
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.
NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.
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.
NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.
The valueOf
function returns a Number, which is this time value.
Returns the difference between local time and UTC time in minutes.
If ms is not specified, this behaves as if ms were specified with the value
getMilliseconds()
.
The length
property of the setSeconds
method is 2.
If ms is not specified, this behaves as if ms were specified with the value
getUTCMilliseconds()
.
The length
property of the setUTCSeconds
method is 2.
If sec is not specified, this 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 length
property of the setMinutes
method is 3.
If sec is not specified, this behaves as if sec were specified with the value
getUTCSeconds()
.
If ms is not specified, this function behaves as if ms were specified with the value return by
getUTCMilliseconds()
.
The length
property of the setUTCMinutes
method is 3.
If min is not specified, this behaves as if min were specified with the value
getMinutes()
.
If sec is not specified, this 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 length
property of the setHours
method is 4.
If min is not specified, this behaves as if min were specified with the value
getUTCMinutes()
.
If sec is not specified, this behaves as if sec were specified with the value
getUTCSeconds()
.
If ms is not specified, this behaves as if ms were specified with the value
getUTCMilliseconds()
.
The length
property of the setUTCHours
method is 4.
If date is not specified, this behaves as if date were specified with the value
getDate()
.
The length
property of the setMonth
method is 2.
If date is not specified, this behaves as if date were specified with the value
getUTCDate()
.
The length
property of the setUTCMonth
method is 2.
If month is not specified, this behaves as if month were specified with the value
getMonth()
.
If date is not specified, this behaves as if date were specified with the value
getDate()
.
The length
property of the setFullYear
method is 3.
If month is not specified, this behaves as if month were specified with the value
getUTCMonth()
.
If date is not specified, this behaves as if date were specified with the value
getUTCDate()
.
The length
property of the setUTCFullYear
method is 3.
This function returns a String value. The contents of the String are implementation-dependent, but are intended to represent the Date 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 15.9.1.15. 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 15.9.1.15 but with a space rather than a “T” used to separate the date and time elements.
This function returns a String value represent the instance in time represented by this Date object. The format of the String is the Date Time string format defined in 15.9.1.15. All fields are present in the String. The time zone is always UTC, denoted by the suffix Z. If the time value of this object is not a finite Number a RangeError exception is thrown.
This function provides a String representation of a Date object for use by JSON.stringify
(15.12.3).
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 object is free to use the
argument key to filter its stringification.
Date instances inherit properties from the Date prototype object and their [[Class]] internal property value is
"Date"
. Date instances also have a [[PrimitiveValue]] internal property.
The [[PrimitiveValue]] internal property is time value represented by this Date object.