Date.UTC() - JavaScript 编辑

The 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.

Syntax

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]]]]])

Parameters

year
A full year.
month Optional
An integer between 0 (January) and 11 (December) representing the month. Since ECMAScript 2017 it defaults to 0 if omitted. (Up until ECMAScript 2016, month was a required parameter. As of ES2017, it no longer is.)
day Optional
An integer between 1 and 31 representing the day of the month. If omitted, defaults to 1.
hour Optional
An integer between 0 and 23 representing the hours. If omitted, defaults to 0.
minute Optional
An integer between 0 and 59 representing the minutes. If omitted, defaults to 0.
second Optional
An integer between 0 and 59 representing the seconds. If omitted, defaults to 0.
millisecond Optional
An integer between 0 and 999 representing the milliseconds. If omitted, defaults to 0.

Return value

A number representing the number of milliseconds for the given date since January 1, 1970, 00:00:00, UTC.

Description

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 20th century (1900 + year). For example, 95 is converted to the year 1995.

The 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 object.

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 instance.

Examples

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));

Specifications

Specification
ECMAScript (ECMA-262)
The definition of 'Date.UTC' in that specification.

Browser compatibility

BCD tables only load in the browser

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

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:70 次

字数:7316

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文