Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Date
  6. UTC()

Date.UTC()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

TheDate.UTC() static method accepts parameters representing the date and time components similar to theDate constructor, but treats them as UTC. It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.

Try it

const utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));const utcDate2 = new Date(Date.UTC(0, 0, 0, 0, 0, 0));console.log(utcDate1.toUTCString());// Expected output: "Fri, 02 Feb 1996 03:04:05 GMT"console.log(utcDate2.toUTCString());// Expected output: "Sun, 31 Dec 1899 00:00:00 GMT"

Syntax

js
Date.UTC(year)Date.UTC(year, monthIndex)Date.UTC(year, monthIndex, day)Date.UTC(year, monthIndex, day, hours)Date.UTC(year, monthIndex, day, hours, minutes)Date.UTC(year, monthIndex, day, hours, minutes, seconds)Date.UTC(year, monthIndex, day, hours, minutes, seconds, milliseconds)

Parameters

year

Integer value representing the year. Values from0 to99 map to the years1900 to1999. All other values are the actual year. See theexample.

monthIndexOptional

Integer value representing the month, beginning with0 for January to11 for December. Defaults to0.

dayOptional

Integer value representing the day of the month. Defaults to1.

hoursOptional

Integer value between0 and23 representing the hour of the day. Defaults to0.

minutesOptional

Integer value representing the minute segment of a time. Defaults to0.

secondsOptional

Integer value representing the second segment of a time. Defaults to0.

millisecondsOptional

Integer value representing the millisecond segment of a time. Defaults to0.

Return value

A number representing thetimestamp of the given date. ReturnsNaN if the date isinvalid.

Description

Years between0 and99 are converted to a year in the 20th century(1900 + year). For example,95 is converted to the year1995.

TheUTC() method differs from theDate() constructor in three 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 aDate object.
  3. When passed a single number,Date.UTC() interprets it as a year instead of a timestamp.

If a parameter is outside of the expected range, theUTC() method updates the other parameters to accommodate the value. For example, if15 is used formonthIndex, the year will be incremented by 1(year + 1) and3 will be used for the month.

BecauseUTC() is a static method ofDate, you always use it asDate.UTC(), rather than as a method of aDate object you created.

Examples

Using Date.UTC()

The following statement creates aDate object with the arguments treated as UTC instead of local:

js
const utcDate = new Date(Date.UTC(2018, 11, 1, 0, 0, 0));

Behavior of Date.UTC() with one argument

Date.UTC() when passed one argument used to have inconsistent behavior, because implementations only kept the behavior consistent with theDate() constructor, which does not interpret a single argument as the year number. Implementations are now required to treat omittedmonthIndex as0, instead of coercing it toNaN.

js
Date.UTC(2017); // 1483228800000

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-date.utc

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp