JavaScript Date

Date.UTC, ISO-8601 UTC strings, and getUTC* accessors. Examples use explicit UTC instants, not the host local timezone.

モジュールページの本文は現在英語です。

Date is an ECMAScript built-in for an instant in time. It is not a DOM clock, and these examples do not use Node.js-only time APIs.

A Date value is a millisecond offset from the Unix epoch. The examples on this page construct instants with Date.UTC, an ISO-8601 string that ends in Z, or a date-only YYYY-MM-DD string (specified as UTC), and they read them with toISOString and the getUTC* methods. They do not call getHours, toString, or toLocaleString without an explicit timeZone, because those follow the host timezone and would not be a stable, portable result.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
Date.UTC, Date.prototype.toISOString, Date.prototype.getUTCHoursBuild a Date from an explicit UTC timestamp or a Z-suffixed ISO string, then read UTC fields. The month index is 0-based.
Date.prototype.getTime and invalid datesDetect an invalid Date with Number.isNaN(date.getTime()). String(date) is Invalid Date; toISOString would throw.

Date.UTC, ISO strings, and UTC accessors

Date.UTC(year, monthIndex, ...) returns a millisecond timestamp. The month argument is 0-based: 0 is January. new Date(timestamp) or new Date("2026-01-01T15:04:05.006Z") builds a Date from that instant. toISOString() always writes a UTC string ending in Z. getUTCFullYear, getUTCMonth, getUTCDate, getUTCHours, and the other getUTC* methods read calendar fields in UTC, so the output does not depend on the machine timezone.

Date.UTC, Date.prototype.toISOString, Date.prototype.getUTCHours

Date.UTC(year, monthIndex, date, hours, minutes, seconds, ms)
new Date(value)
date.toISOString()
date.getUTCFullYear()
date.getUTCMonth()

Build a Date from an explicit UTC timestamp or a Z-suffixed ISO string, then read UTC fields. The month index is 0-based.

ParameterDescription
year, monthIndex, date, hours, minutes, seconds, msDate.UTC takes a year and a 0-based month, then optional day and time fields, all interpreted as UTC. day defaults to 1; the time fields default to 0.
valueA millisecond timestamp, or an ISO-8601 string. A string with a Z or numeric offset is UTC. A date-only YYYY-MM-DD string is also specified as UTC midnight. A date-time string without an offset is specified as local time.

Returns: Date.UTC returns a number of milliseconds. toISOString returns a string such as 2026-01-01T15:04:05.006Z. The getUTC* methods return numbers; getUTCMonth returns 0 for January.

Throws

  • RangeError — toISOString throws if the Date is invalid, for example after new Date("not a date"). The next example shows how to detect that case without calling toISOString.
  • These outputs were measured in Node.js 22. They do not exercise the host local timezone.
  • Date.parse and the Date constructor treat a complete ISO-8601 string with a Z or numeric offset, and a date-only YYYY-MM-DD string, as UTC. ECMAScript defines that date-only form as UTC midnight. A date-time string without an offset is specified as local time, so converting it to UTC depends on the host timezone and is not asserted here. Non-standard date strings, such as "01/01/2026", are implementation-defined and are not used as expected output.
  • new Date() with no arguments uses the current time and is not deterministic, so it is not used here.
javascript
const utc = new Date(Date.UTC(2026, 0, 1, 15, 4, 5, 6));
console.log(utc.toISOString());
console.log(
  utc.getUTCFullYear(),
  utc.getUTCMonth(),
  utc.getUTCDate(),
  utc.getUTCHours(),
  utc.getUTCMinutes(),
  utc.getUTCSeconds(),
  utc.getUTCMilliseconds(),
);
const parsed = new Date("2026-01-01T15:04:05.006Z");
console.log(parsed.toISOString(), parsed.getTime() === utc.getTime());
console.log(new Date("2026-01-01").toISOString());

Expected output

2026-01-01T15:04:05.006Z
2026 0 1 15 4 5 6
2026-01-01T15:04:05.006Z true
2026-01-01T00:00:00.000Z

Related: Intl.DateTimeFormat with a timeZone

MDN documentation →

Invalid Date

If the input cannot be parsed as a time, the Date is invalid. getTime() is then NaN, Number.isNaN(date.getTime()) is true, and String(date) is the specified string Invalid Date. Do not call toISOString() on an invalid Date; it throws RangeError. Prefer the getTime() check before formatting.

Date.prototype.getTime and invalid dates

new Date(dateString)
date.getTime()

Detect an invalid Date with Number.isNaN(date.getTime()). String(date) is Invalid Date; toISOString would throw.

ParameterDescription
dateStringIf the string is not a parseable time, the Date is invalid. The example uses a string that is not a date.

Returns: getTime returns a finite millisecond timestamp for a valid Date, or NaN when the Date is invalid.

Throws

  • RangeError — Thrown by toISOString when the Date is invalid. The example avoids that call and checks getTime instead.
  • An invalid Date is still a Date object. Comparisons such as date instanceof Date stay true; the time value is what fails.
javascript
const invalid = new Date("not a date");
console.log(Number.isNaN(invalid.getTime()), String(invalid));
console.log(invalid instanceof Date);

Expected output

true Invalid Date
true

MDN documentation →