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.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

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
An integer between 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 可选
An integer between 1 and 31 representing the day of the month. If omitted, defaults to 1 .
hour 可选
An integer between 0 and 23 representing the hours. If omitted, defaults to 0 .
minute 可选
An integer between 0 and 59 representing the minutes. If omitted, defaults to 0 .
second 可选
An integer between 0 and 59 representing the seconds. If omitted, defaults to 0 .
millisecond 可选
An integer between 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:

  1. Date.UTC() uses universal time instead of the local time.
  2. 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 实例。

范例

Using Date.UTC()

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.

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
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

图例

完整支持

完整支持

Compatibility notes

Date.UTC() with fewer than two arguments

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
					

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Date
  3. 方法
    1. Date.UTC()
    2. Date.now()
    3. Date.parse()
    4. Date.prototype.getDate()
    5. Date.prototype.getDay()
    6. Date.prototype.getFullYear()
    7. Date.prototype.getHours()
    8. Date.prototype.getMilliseconds()
    9. Date.prototype.getMinutes()
    10. Date.prototype.getMonth()
    11. Date.prototype.getSeconds()
    12. Date.prototype.getTime()
    13. Date.prototype.getTimezoneOffset()
    14. Date.prototype.getUTCDate()
    15. Date.prototype.getUTCDay()
    16. Date.prototype.getUTCFullYear()
    17. Date.prototype.getUTCHours()
    18. Date.prototype.getUTCMilliseconds()
    19. Date.prototype.getUTCMinutes()
    20. Date.prototype.getUTCMonth()
    21. Date.prototype.getUTCSeconds()
    22. Date.prototype.getYear()
    23. Date.prototype.setDate()
    24. Date.prototype.setFullYear()
    25. Date.prototype.setHours()
    26. Date.prototype.setMilliseconds()
    27. Date.prototype.setMinutes()
    28. Date.prototype.setMonth()
    29. Date.prototype.setSeconds()
    30. Date.prototype.setTime()
    31. Date.prototype.setUTCDate()
    32. Date.prototype.setUTCFullYear()
    33. Date.prototype.setUTCHours()
    34. Date.prototype.setUTCMilliseconds()
    35. Date.prototype.setUTCMinutes()
    36. Date.prototype.setUTCMonth()
    37. Date.prototype.setUTCSeconds()
    38. Date.prototype.setYear()
    39. Date.prototype.toDateString()
    40. Date.prototype.toGMTString()
    41. Date.prototype.toISOString()
    42. Date.prototype.toJSON()
    43. Date.prototype.toLocaleDateString()
    44. Date.prototype.toLocaleString()
    45. Date.prototype.toLocaleTimeString()
    46. Date.prototype.toSource()
    47. Date.prototype.toString()
    48. Date.prototype.toTimeString()
    49. Date.prototype.toUTCString()
    50. Date.prototype.valueOf()
    51. Date.prototype[@@toPrimitive]
  4. 继承:
  5. Function
  6. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  7. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  8. Object
  9. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  10. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()