The Date.UTC()
method accepts the same parameters as the longest form of the constructor, and returns the number of milliseconds in a Date
object since January 1, 1970, 00:00:00, universal time.
Date.UTC(year, month[, day[, hour[, minute[, second[, millisecond]]]]])
year
month
day
hour
minute
second
millisecond
A number representing the number of milliseconds in the given Date
object since January 1, 1970, 00:00:00, universal time.
UTC()
takes comma-delimited date parameters and returns the number of milliseconds between January 1, 1970, 00:00:00, universal time and the time you specified.
You should specify a full year for the year
; for example, 1998. If a year between 0 and 99 is specified, the method converts the year to a year in the 20th century (1900 + year)
; for example, if you specify 95, the year 1995 is used.
The 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
object.If a parameter you specify is outside of the expected range, the UTC()
method updates the other parameters to allow for your number. For example, if you use 15 for month, the year will be incremented by 1 (year + 1)
, and 3 will be used for the month.
Because UTC()
is a static method of Date
, you always use it as Date.UTC()
, rather than as a method of a Date
object you created.
Date.UTC()
The following statement creates a Date
object using UTC instead of local time:
var utcDate = new Date(Date.UTC(96, 11, 1, 0, 0, 0));
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (ECMA-262) The definition of 'Date.UTC' in that specification. | Draft | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Date.UTC' in that specification. | Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'Date.UTC' in that specification. | Standard | |
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.0. |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | ? |
Date.UTC
with fewer than two argumentsWhen providing less than two arguments to Date.UTC
, NaN
is returned. This behavior is specified in ECMAScript 2017. Engines who 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
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC