javascript date

A javascript date is a Date object that stores one instant as milliseconds since 1970-01-01T00:00:00.000Z, the Unix epoch. That count ignores leap seconds. Methods such as getMonth() then read the same instant in the host time zone.

const now = new Date();
now.getTime();

new Date() with no arguments reads the current time, the same clock Date.now() uses. getTime() returns the milliseconds stored on now. Date.now() returns that kind of number directly and does not build a Date.

new Date(0).toISOString(); // "1970-01-01T00:00:00.000Z"

A timestamp of 0 is midnight at the start of 1 January 1970, UTC. toISOString() prints that instant in UTC, and the Z at the end means UTC.

Four ways to construct a Date

new Date() has four forms you will use. The thing to watch is a single argument. One number is a timestamp in milliseconds. It is a year only when you also pass a month.

new Date();
new Date(2026);
new Date("2026-02-01T00:00:00Z");
new Date(2026, 1, 1);

The first call reads the current time. The second call is 2026 milliseconds after the epoch, which toISOString() prints as "1970-01-01T00:00:02.026Z". The third call parses a date time string. That string is 1 February 2026 at 00:00 UTC. The fourth call takes local components. You passed the year 2026, the month index 1, and the day 1. The hour, minute, second, and millisecond default to 0. Month 1 is February, so this date is 1 February 2026 in the host time zone.

A year from 0 to 99 means 1900 to 1999. new Date(99, 0, 1) is 1 January 1999 local, and getFullYear() returns 1999.

Date(2026, 1, 1), written without new, returns a string for the current time. The arguments are ignored. The string matches what new Date().toString() returns.

The zero-indexed month

const feb = new Date(2026, 1, 1);
feb.getFullYear(); // 2026
feb.getMonth();    // 1
feb.getDate();     // 1

new Date(2026, 1, 1) stores local midnight on 1 February 2026. getFullYear(), getMonth(), and getDate() read those local components back. getMonth() returns 1. January is 0 and December is 11. The local midnight you wanted in January is new Date(2026, 0, 1).

Date arithmetic and overflow

const due = new Date(2026, 0, 28);
due.setDate(due.getDate() + 7);
due.getMonth(); // 1
due.getDate();  // 4

new Date(2026, 0, 28) is 28 January 2026 at local midnight. getDate() reads 28, and setDate(35) writes day 35 onto due. January has 31 days, so the extra days roll into February. After setDate, due is 4 February 2026, and getHours() is still 0 because the local time of day is kept. setDate changes due and returns the new timestamp number.

The constructor rolls the same way. new Date(1990, 12, 1) is 1 January 1991 local. Month 12 is past December, which is month 11, so the year increases and the month becomes January, getMonth() 0. new Date(2020, 5, 0) is 31 May 2020 local. Day 0 borrows from the previous month. Month 5 is June, so the previous month is May, and getDate() is 31.

Formatting dates

toISOString() is the string to store. It is UTC, it ends in Z, and Date.parse() accepts it. toLocaleDateString() is the string to show on a page. You pass the locale and the calendar fields you want.

const instant = new Date("2026-01-15T13:04:05.000Z");
instant.toISOString(); // "2026-01-15T13:04:05.000Z"
instant.toLocaleDateString("en-US", {
  timeZone: "UTC",
  year: "numeric",
  month: "long",
  day: "numeric",
}); // "January 15, 2026"

new Date("2026-01-15T13:04:05.000Z") parses that UTC instant. toISOString() prints the same characters back. toLocaleDateString("en-US", ...) with timeZone: "UTC" formats the UTC calendar date, so the result is January 15, 2026 and the host zone stays out of it.

JSON.stringify calls toJSON() on a Date. For a valid date, toJSON() returns the same UTC string as toISOString().

JSON.stringify(new Date(0)); // '"1970-01-01T00:00:00.000Z"'

An invalid date is a Date whose timestamp is NaN. String(new Date("nope")) is "Invalid Date". toISOString() on that object throws RangeError.

Time zones and the Z suffix

Date.parse() and new Date(dateString) share one parser. In the standard date time format, a missing offset means UTC for a date-only string and local time for a string that includes a time. Z means UTC. An offset such as -05:00 is that many hours from UTC.

Date.parse("2019-01-01");                // 1546300800000
Date.parse("2019-01-01T00:00:00.000Z");  // 1546300800000
Date.parse("2019-01-01T00:00:00-05:00"); // 1546318800000

"2019-01-01" is date-only, so it is 1 January 2019 at 00:00 UTC. The Z form is that same instant, and both calls return 1546300800000. "2019-01-01T00:00:00-05:00" is midnight at five hours west of UTC, which is 05:00 UTC. The timestamp is 18000000 milliseconds later, five hours.

"2019-01-01T00:00:00" has a time and no offset, so it is midnight in the host zone. Date.parse of that string equals 1546300800000 when the host offset for that instant is 0. A string outside this format is left to the engine. When the engine cannot parse it, Date.parse() returns NaN.

Date.now() and performance.now()

Date.now() is the wall clock in whole milliseconds since the epoch. It follows the system clock, so a later call can return a smaller number after the clock steps backward.

performance.now() is a fractional number of milliseconds since Performance.timeOrigin. In a window, that origin is the start of the document navigation. The clock moves forward, so subtracting two readings gives an elapsed duration. The number is a duration from navigation, so you store a Date timestamp when you need an instant on a timeline.

const start = performance.now();
const elapsed = performance.now() - start;

start is the time since navigation, in milliseconds. elapsed is how long the second call took to run. Browsers coarsen performance.now() to 5 microseconds in a cross-origin isolated document and to 100 microseconds otherwise.

The Temporal API

Temporal is the replacement for Date. It is a namespace, so you call Temporal.PlainDate.from and you do not write new Temporal(). A PlainDate is a calendar date. Its month numbers start at 1.

const date = Temporal.PlainDate.from("2026-01-28");
const later = date.add({ days: 7 });
later.toString(); // "2026-02-04"
later.month;      // 2
date.toString();  // "2026-01-28"

Temporal.PlainDate.from("2026-01-28") reads 28 January 2026. There is no time and no time zone on date. add({ days: 7 }) returns a new PlainDate seven calendar days later, 4 February 2026. later.month is 2 because February is month 2 in Temporal. date is unchanged, and date.toString() is still "2026-01-28".

Temporal is a stage 4 proposal. The proposal lists it as shipped in Firefox 139, Chrome 144, and Node 26. MDN marks it as limited availability. When Temporal is missing, Temporal.PlainDate throws ReferenceError.

If this article was helpful, tweet it!