← Back to JavaScript, page 8

javascript-0155 · Easy fresher

How do you create, read, format, and compare dates with the Date object, and what are the basic gotchas?

A Date stores one number, the milliseconds since 1 January 1970 UTC, and shows it in local time or UTC through different methods. Months are counted from 0, the date-only string "2026-03-05" is parsed as UTC while "2026-03-05T00:00" is local time, and comparing two dates with == compares references, so compare getTime values. Use toISOString for storage and Intl for display.

Full answer

A Date is a wrapper around a single timestamp: the number of milliseconds since the Unix epoch (1 January 1970, 00:00 UTC). Date.now() and date.getTime() return that number.

Creating dates:

  • new Date() is now.
  • new Date(ms) from a timestamp.
  • new Date('2026-03-05T10:30:00Z') from an ISO string. A trailing Z means UTC, and an offset such as +05:30 is also exact.
  • new Date(2026, 2, 5, 10, 30) from parts, in local time. The month is zero-based: 0 is January, so 2 is March. This is the most common beginner bug. Day numbers start at 1.

Reading: getFullYear(), getMonth() (0 to 11), getDate() (day of month), getDay() (0 is Sunday), getHours(), and so on give local time, and the getUTC... versions give UTC. Never use getYear().

Formatting: toISOString() gives a UTC string such as 2026-03-05T10:30:00.000Z, which is the best format for storing and sending. toLocaleDateString(locale, options) and Intl.DateTimeFormat format for people and handle languages and time zones. toString() depends on the machine and should not be used for storage.

Parsing gotchas: a date-only ISO string ('2026-03-05') is treated as UTC midnight, so in a time zone behind UTC the local date can be the day before. A date-time string without an offset ('2026-03-05T00:00') is local time. Other string formats are implementation-dependent, so avoid them. An invalid date has NaN as its time: check with Number.isNaN(date.getTime()).

Comparing: date1 < date2 works because dates convert to numbers, but date1 === date2 compares references, and == too. Use date1.getTime() === date2.getTime().

Arithmetic: dates are mutable. setDate(getDate() + 7) adds a week and handles month overflow. Copy a date with new Date(date) before changing it. Adding 24 * 60 * 60 * 1000 can be wrong across daylight saving changes.

For time zone and calendar-heavy work, use Intl, or the newer Temporal API when it is available.

javascript
const fixed = new Date('2026-03-05T10:30:00Z');
console.log(fixed.getTime(), fixed.toISOString(), fixed.toJSON() === fixed.toISOString());
console.log('month is zero-based:', new Date(2026, 2, 5).getMonth(), '(March)', '| UTC month of fixed:', fixed.getUTCMonth());
console.log('weekday (0 = Sunday):', fixed.getUTCDay(), '| day of month:', fixed.getUTCDate(), '| year:', fixed.getUTCFullYear());

console.log(new Date(Date.UTC(2026, 11, 31, 23, 59, 59)).toISOString());
console.log(new Date(0).toISOString(), new Date(86400000).toISOString());

const dateOnly = new Date('2026-03-05');
console.log('date-only string is UTC midnight:', dateOnly.toISOString());
const invalid = new Date('not a date');
console.log('invalid:', Number.isNaN(invalid.getTime()), String(invalid));

const a = new Date('2026-03-05T10:30:00Z');
const b = new Date('2026-03-05T10:30:00Z');
console.log('===:', a === b, '| ==:', a == b, '| getTime:', a.getTime() === b.getTime(), '| less than:', a < new Date('2026-03-06T00:00:00Z'));

const start = new Date(Date.UTC(2026, 0, 31));
const moved = new Date(start);
moved.setUTCDate(moved.getUTCDate() + 30);
console.log('mutation copy:', start.toISOString().slice(0, 10), '->', moved.toISOString().slice(0, 10));
const overflow = new Date(Date.UTC(2026, 0, 31));
overflow.setUTCMonth(1);
console.log('Jan 31 plus one month overflows to:', overflow.toISOString().slice(0, 10));

const diffDays = (x, y) => Math.round((y - x) / 86400000);
console.log('days between:', diffDays(new Date('2026-03-01T00:00:00Z'), new Date('2026-12-25T00:00:00Z')));

const format = new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'short', timeZone: 'Asia/Kolkata' });
console.log(format.format(fixed));
console.log(new Intl.DateTimeFormat('en-US', { dateStyle: 'medium', timeZone: 'UTC' }).format(fixed));

Run with Node.js v24.22.1 as js-155.js; two runs gave identical output. Output: 1772706600000 2026-03-05T10:30:00.000Z true month is zero-based: 2 (March) | UTC month of fixed: 2 weekday (0 = Sunday): 4 | day of month: 5 | year: 2026 2026-12-31T23:59:59.000Z 1970-01-01T00:00:00.000Z 1970-01-02T00:00:00.000Z date-only string is UTC midnight: 2026-03-05T00:00:00.000Z invalid: true Invalid Date ===: false | ==: false | getTime: true | less than: true mutation copy: 2026-01-31 -> 2026-03-02 Jan 31 plus one month overflows to: 2026-03-03 days between: 299 Thursday, 5 March 2026 at 16:00 Mar 5, 2026

Key points

  • A Date is one number of milliseconds since 1970 UTC; local and UTC getters show it differently.
  • Months start at 0; getDay is the weekday, getDate is the day of the month.
  • Date-only ISO strings are UTC, date-time without offset is local; avoid other formats.
  • Compare with getTime, not ===; check validity with Number.isNaN(date.getTime()).
  • Store with toISOString; display with Intl; copy dates before mutating.

Common mistakes

  • Using getMonth as if January were 1.
  • Parsing non-ISO date strings and getting different results in different browsers.
  • Comparing dates with === or ==.
  • Mutating a date that other code also uses.

Follow-ups

  • Why does the string 2026-03-05 sometimes show the previous day in local time?
  • How would you add one month to a date and keep the day at the end of the month?
  • Why prefer toISOString for storing dates?

Tips

  • Start with the epoch idea: one number.
  • Name the zero-based month early; it is the classic bug.
  • Recommend ISO strings for storage and Intl for display.