← Back to JavaScript, page 3

javascript-0049 · Medium mid

What are the common pitfalls of the Date object in JavaScript, and how do you work with time zones safely?

Date stores a single UTC timestamp in milliseconds, but many of its methods read or format it in the machine's local time zone. Months are zero-based, date-only ISO strings parse as UTC while date-time strings without an offset parse as local time, and month arithmetic overflows. Use UTC methods or Intl.DateTimeFormat with an explicit timeZone, and store timestamps in UTC.

Full answer

A Date is nothing more than a number: milliseconds since 1 January 1970 UTC. There is no time zone inside it. What changes is how it is read. getHours() converts to the local zone of the machine, while getUTCHours() does not, and toISOString() always prints UTC.

Frequent pitfalls:

  • Months run from 0 to 11, but days run from 1 to 31. new Date(2026, 0, 15) is 15 January.
  • Parsing is inconsistent. new Date('2026-01-15') (date only, ISO format) is read as UTC midnight, but new Date('2026-01-15T00:00:00') (no offset) is read as local time. Always include Z or an explicit offset such as +05:30 in stored strings.
  • Date setters overflow instead of failing. Setting the month to February on 31 January gives a date in March.
  • An invalid date does not throw. It becomes an Invalid Date object whose getTime() is NaN.
  • Adding 24 hours of milliseconds is not the same as adding one calendar day in a zone with daylight saving changes.
  • A Date object is mutable, so copy it before changing it.

For safe work: store and send UTC timestamps or ISO strings with an offset; do calendar arithmetic with UTC methods; and display with Intl.DateTimeFormat and an explicit timeZone option, for example Asia/Kolkata. Do not hard-code a zone offset, since offsets change with daylight saving time.

Temporal is the modern replacement API with separate types for instants, zoned times, and plain dates. Where it is not available yet, a library can fill the gap.

javascript
const january15 = new Date(Date.UTC(2026, 0, 15, 10, 30));
console.log(january15.toISOString(), january15.getTime());

const dateOnly = new Date('2026-01-15');
console.log(dateOnly.toISOString());

const withOffset = new Date('2026-01-15T10:30:00+05:30');
console.log(withOffset.toISOString());

const invalid = new Date('not a date');
console.log(String(invalid), Number.isNaN(invalid.getTime()));

const endOfJanuary = new Date(Date.UTC(2026, 0, 31));
endOfJanuary.setUTCMonth(1); // February has no 31st
console.log(endOfJanuary.toISOString().slice(0, 10));

const start = Date.UTC(2026, 0, 1);
const end = Date.UTC(2026, 2, 1);
console.log((end - start) / 86400000, 'days between 1 Jan and 1 Mar 2026');

const meeting = new Date('2026-03-10T14:00:00Z');
for (const timeZone of ['Asia/Kolkata', 'America/New_York', 'UTC']) {
  const text = new Intl.DateTimeFormat('en-GB', {
    timeZone,
    dateStyle: 'medium',
    timeStyle: 'short',
  }).format(meeting);
  console.log(timeZone.padEnd(17), text);
}

const original = new Date(Date.UTC(2026, 5, 1));
const next = new Date(original);
next.setUTCDate(next.getUTCDate() + 30);
console.log(original.toISOString().slice(0, 10), next.toISOString().slice(0, 10));

Run with Node.js v24.22.1 as js-49.js; two runs gave identical output. Output: 2026-01-15T10:30:00.000Z 1768473000000 2026-01-15T00:00:00.000Z 2026-01-15T05:00:00.000Z Invalid Date true 2026-03-03 59 days between 1 Jan and 1 Mar 2026 Asia/Kolkata 10 Mar 2026, 19:30 America/New_York 10 Mar 2026, 10:00 UTC 10 Mar 2026, 14:00 2026-06-01 2026-07-01

Key points

  • A Date is a UTC timestamp; local time appears only when reading or formatting.
  • Months are zero-based; setters overflow into the next month.
  • Date-only ISO strings parse as UTC; date-time strings without an offset parse as local.
  • Invalid dates produce NaN rather than an exception.
  • Store UTC; format with Intl.DateTimeFormat and an explicit timeZone.

Common mistakes

  • Forgetting that months start at 0.
  • Parsing a date-only string and a date-time string and assuming both use the same zone.
  • Adding 86,400,000 ms to move a calendar day across a daylight saving change.
  • Mutating a Date that is shared, such as one stored in state.

Follow-ups

  • Why does new Date('2026-01-15') show the previous day for users west of UTC when formatted in local time?
  • How would you display a time for a user in a time zone different from the server's?
  • What problems does the Temporal API solve?

Tips

  • Start with "a Date is a UTC timestamp"; every pitfall then follows from it.
  • Recommend one concrete practice: store UTC, format with an explicit timeZone.
  • If asked about libraries, mention Temporal and say you pick based on browser and runtime support.