UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ Date/ Date.UTC()

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

Syntax

Date.UTC(year)
Date.UTC(year, monthIndex)
Date.UTC(year, monthIndex, day)
Date.UTC(year, monthIndex, day, hour)
Date.UTC(year, monthIndex, day, hour, minute)
Date.UTC(year, monthIndex, day, hour, minute, second)
Date.UTC(year, monthIndex, day, hour, minute, second, millisecond)

Parameters

Return value

A number representing the timestamp of the given date. Returns NaN if the date is invalid.

Description

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 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 a Date 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, the UTC() method updates the other parameters to accommodate the value. For example, if 15 is used for monthIndex, 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.

Examples

Using Date.UTC()

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

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 the Date() constructor, which does not interpret a single argument as the year number. Implementations are now required to treat omitted monthIndex as 0, instead of coercing it to NaN.

Date.UTC(2017); // 1483228800000

Specifications

Browser compatibility

See also