Date.UTC()
method accepts parameters similar to the
Date
constructor, but treats them as UTC. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
Since ECMAScript 2017:
Date.UTC(year[, month[, day[, hour[, minute[, second[, millisecond]]]]]])
ECMAScript 2016 and earlier:
(
month
used to be required)
Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]])
year
A full year.
month
0
(January) and
11
(December) representing the month.
(Up through ECMAScript 2016,
month
was a required parameter. As of ES2017, it no longer is.)
day
可选
1
and
31
representing the day of the month. If omitted, defaults to
1
.
hour
可选
0
and
23
representing the hours. If omitted, defaults to
0
.
minute
可选
0
and
59
representing the minutes. If omitted, defaults to
0
.
second
可选
0
and
59
representing the seconds. If omitted, defaults to
0
.
millisecond
可选
0
and
999
representing the milliseconds. If omitted, defaults to
0
.
A number representing the number of milliseconds for the given date since January 1, 1970, 00:00:00, UTC.
UTC()
takes comma-delimited date and time parameters and returns the number of milliseconds between January 1, 1970, 00:00:00, universal time and the specified date and time.
Years between
0
and
99
are converted to a year in the 20
th
century
(1900 + year)
。例如,
95
is converted to the year
1995
.
UTC()
method differs from the
Date
constructor in two ways:
Date.UTC()
uses universal time instead of the local time.
Date.UTC()
returns a time value as a number instead of creating a
Date
对象。
If a parameter is outside of the expected range, the
UTC()
method updates the other parameters to accommodate the value. For example, if
15
is used for
month
, the year will be incremented by 1
(
year
+ 1)
and
3
will be used for the month.
UTC()
is a static method of
Date
, so it's called as
Date.UTC()
rather than as a method of a
Date
实例。
The following statement creates a
Date
object with the arguments treated as UTC instead of local:
let utcDate = new Date(Date.UTC(2018, 11, 1, 0, 0, 0));
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Date.UTC' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
UTC
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
When providing less than two arguments to
Date.UTC()
, ECMAScript 2017 requires that
NaN
is returned. Engines that weren't supporting this behavior have been updated (see
bug 1050755
,
ecma-262 #642
).
Date.UTC(); Date.UTC(1); // Safari: NaN // Chrome/Opera/V8: NaN // Firefox <54: non-NaN // Firefox 54+: NaN // IE: non-NaN // Edge: NaN
Date
Date.UTC()
Date.now()
Date.parse()
Date.prototype.getDate()
Date.prototype.getDay()
Date.prototype.getFullYear()
Date.prototype.getHours()
Date.prototype.getMilliseconds()
Date.prototype.getMinutes()
Date.prototype.getMonth()
Date.prototype.getSeconds()
Date.prototype.getTime()
Date.prototype.getTimezoneOffset()
Date.prototype.getUTCDate()
Date.prototype.getUTCDay()
Date.prototype.getUTCFullYear()
Date.prototype.getUTCHours()
Date.prototype.getUTCMilliseconds()
Date.prototype.getUTCMinutes()
Date.prototype.getUTCMonth()
Date.prototype.getUTCSeconds()
Date.prototype.getYear()
Date.prototype.setDate()
Date.prototype.setFullYear()
Date.prototype.setHours()
Date.prototype.setMilliseconds()
Date.prototype.setMinutes()
Date.prototype.setMonth()
Date.prototype.setSeconds()
Date.prototype.setTime()
Date.prototype.setUTCDate()
Date.prototype.setUTCFullYear()
Date.prototype.setUTCHours()
Date.prototype.setUTCMilliseconds()
Date.prototype.setUTCMinutes()
Date.prototype.setUTCMonth()
Date.prototype.setUTCSeconds()
Date.prototype.setYear()
Date.prototype.toDateString()
Date.prototype.toGMTString()
Date.prototype.toISOString()
Date.prototype.toJSON()
Date.prototype.toLocaleDateString()
Date.prototype.toLocaleString()
Date.prototype.toLocaleTimeString()
Date.prototype.toSource()
Date.prototype.toString()
Date.prototype.toTimeString()
Date.prototype.toUTCString()
Date.prototype.valueOf()
Date.prototype[@@toPrimitive]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()