JavaScript Dates: A Practical Guide

JavaScript's Date represents an instant as milliseconds from the Unix epoch. Formatting and parsing choices determine how that instant appears to people.

JavaScript Dates: A Practical Guide: Date.now(), Milliseconds since epoch; toISOString(), UTC representation; Local display, Uses device time zone.

What a Date value represents

A JavaScript Date stores a numeric time value measured in milliseconds relative to 1 January 1970 at 00:00 UTC. A single value can be displayed as UTC or as local time without changing the instant it represents. A browser normally uses the device's configured time zone when producing local output.

For example, new Date(0).toISOString() returns 1970-01-01T00:00:00.000Z. The same instant may be displayed as an earlier local calendar date west of UTC. That is a formatting difference, not evidence that the underlying time value has changed. See the Unix epoch guide for the shared starting point.

Milliseconds, seconds and precision

Date.now() returns milliseconds. Many APIs describe Unix timestamps in seconds, so interpreting one unit as the other changes the result dramatically. A value in seconds can be converted to milliseconds by multiplying by 1,000, provided it is within the supported Date range. When sending data to another system, document the unit instead of guessing from the number of digits.

A Date has millisecond resolution, but that does not prove the device clock is accurate to a millisecond. The platform may reduce timer precision or have an unsynchronized clock. For elapsed performance measurement inside a page, a monotonic timer such as performance.now() is usually more appropriate than subtracting two wall-clock readings.

Parsing date-only and date-time strings

The built-in date-time string format has an important distinction: a date-only string such as 2026-05-04 is interpreted as UTC, while a date-time without an offset such as 2026-05-04T09:30:00 is interpreted as local time. If a date-only value is displayed in a negative offset, the resulting local date can be the previous day.

A birthday or holiday is often a calendar date, not an instant at midnight UTC. Keep its year, month and day as a date-only value when no time zone is intended. For an exact event, include Z or an explicit numeric offset in the exchanged string. Avoid relying on implementation-dependent parsing of informal date strings.

Formatting and named time zones

toISOString() returns a UTC string ending in Z. Methods such as toLocaleString() can format a value for a language and region. To display the same instant in a particular location, use Intl.DateTimeFormat with a supported IANA identifier such as Europe/London, rather than adding a hard-coded offset.

For future local schedules, retain the intended local time and named zone alongside any resolved instant. Clock changes can make a local time missing or repeated, and future rules can change. Read our time-zone guide and consult the MDN Date reference for the precise behavior of the methods you use.

A practical checklist

Validate the input before formatting it: an invalid Date cannot produce a useful ISO string. Identify whether the input is a date, an instant or a recurring local schedule. Record the timestamp unit, choose UTC for interchange when appropriate, and specify the desired display zone. Test dates near midnight and daylight-saving transitions.

Try the timestamp converter to inspect an instant in UTC and local time. For a comparison of timestamp units and accuracy, continue to timestamp precision.

Three constructors, three meanings

new Date("2026-05-04")          // date-only string: UTC midnight
new Date("2026-05-04T00:00")    // date-time without offset: local midnight
new Date(2026, 4, 4)            // numeric parts: local midnight, month is zero-based

Months count from 0, so 4 means May. Days of the week count from Sunday as 0 in getDay(). getTimezoneOffset() returns minutes with the opposite sign to the usual notation: it gives 300 on UTC−5 and -330 on UTC+5:30. The live clock on this site's home page flips that sign before showing the offset.

Formatting with Intl

new Intl.DateTimeFormat("en-GB", {
  timeZone: "Asia/Tokyo", dateStyle: "full", timeStyle: "short"
}).format(new Date("2026-12-05T08:30:00Z"));
// "Saturday 5 December 2026 at 17:30"

Intl.supportedValuesOf("timeZone") lists the zone names the browser knows. The time zone converter builds its drop-down from that list. To turn a local time in another zone into an instant, it uses formatToParts to read the zone's offset at that moment, then corrects once more in case the offset changed.

Mistakes worth catching in review

  • date1 == date2 compares object identity, not time. Compare getTime() values instead.
  • setMonth() and setDate() roll over. Setting month 1 on 31 January gives 3 March, not the end of February.
  • Mutating a shared Date changes it for every reference. Copy it with new Date(d) first.

The newer Temporal API has separate types for plain dates, zoned date-times and instants. It has started shipping in browsers, so check MDN's compatibility table before relying on it without a polyfill.

Common questions

Why does new Date("2026-05-04") show 3 May?

The string is read as UTC midnight. In a zone behind UTC that instant is still the evening of 3 May. Use new Date(2026, 4, 4) for local midnight, or keep the value as a date-only string.

How do I get a Unix timestamp in seconds?

Math.floor(Date.now() / 1000). Date.now() returns milliseconds.

Sources and further reading