Dates

Topic 2 of 3 in Built-in objects6 min readBeginnerFree course

Overview

The Date object represents a single moment in time.

JavaScript
const now = new Date();
const launch = new Date(2026, 11, 25);          // 25 December 2026 — months start at 0!
const fromText = new Date('2026-12-25T09:30:00');
const fromMs = new Date(0);                     // 1 January 1970, 00:00 UTC

Dates in JavaScript are famous for a few quirks. Once you know them, they’re manageable.

How dates are stored

Under the hood a date is just one number: the milliseconds since midnight on 1 January 1970, UTC (the “Unix epoch”). Everything else — the year, month, hour — is calculated from that number when you ask for it.

A timeline from 1 January 1970 at 0 milliseconds to 25 December 2026 at 1,798,156,800,000 milliseconds. Below, the months January to December are numbered 0 to 11.

JavaScript
launch.getTime(); // e.g. 1798137000000 (depends on your time zone)
Date.now();       // milliseconds right now — handy for timing things

Creating dates

Code Meaning
new Date() now
new Date(2026, 11, 25) 25 Dec 2026, midnight local time (month 11!)
new Date(2026, 11, 25, 9, 30) …at 09:30 local time
new Date('2026-12-25') date-only ISO string → midnight UTC
new Date('2026-12-25T09:30:00') date and time → local time
new Date('2026-12-25T09:30:00Z') Z means UTC
new Date(ms) from a millisecond timestamp

Months start at 0, days start at 1. new Date(2026, 0, 1) is 1 January. This trips up everyone at least once.

Stick to ISO strings (YYYY-MM-DD…) when parsing text. Other formats such as '12/03/2026' are read differently in different browsers — is that 12 March or 3 December?

Invalid input doesn’t throw; you get an “Invalid Date” whose time is NaN:

JavaScript
const bad = new Date('not a date');
Number.isNaN(bad.getTime()); // true — check before using it

Reading parts

JavaScript
launch.getFullYear(); // 2026
launch.getMonth();    // 11 (December)
launch.getDate();     // 25 (day of the month)
launch.getDay();      // day of the week, 0 = Sunday
launch.getHours();    // 0
launch.getMinutes();  // 0

These use the user’s local time zone. Each has a UTC twin — getUTCFullYear(), getUTCHours() and so on.

Avoid getYear() — it’s an old method that returns the year minus 1900.

Date maths

Under the hood a date is a number of milliseconds. Subtracting two dates gives the gap between them:

JavaScript
const msPerDay = 24 * 60 * 60 * 1000;
const days = Math.round((launch - now) / msPerDay);

To move a date, use the set… methods. They roll over correctly at month and year ends:

JavaScript
const nextWeek = new Date(now);
nextWeek.setDate(now.getDate() + 7); // handles month ends for you

const d = new Date(2026, 0, 31);   // 31 January
d.setDate(d.getDate() + 1);        // 1 February

Dates are mutable. setDate changes the Date object itself. Copy it first with new Date(original) if other code still needs the old value.

Comparing dates works with < and >, but not with ===, because two Date objects are different objects:

JavaScript
const a = new Date(2026, 0, 1);
const b = new Date(2026, 0, 1);
a < b;                       // false
a === b;                     // false — different objects!
a.getTime() === b.getTime(); // true

Formatting

JavaScript
launch.toLocaleDateString('en-IN', { day: 'numeric', month: 'long', year: 'numeric' });
// '25 December 2026'

new Intl.DateTimeFormat('en-US', { weekday: 'short', hour: 'numeric', minute: '2-digit' })
  .format(launch);

A few useful built-in formats:

JavaScript
launch.toISOString();        // '2026-12-24T18:30:00.000Z' — UTC, for storing and sending
launch.toLocaleDateString(); // the user's usual date format
launch.toLocaleTimeString(); // the user's usual time format

Relative times

Intl.RelativeTimeFormat produces phrases like “in 3 days” or “2 hours ago” in any language:

JavaScript
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
rtf.format(-1, 'day');  // 'yesterday'
rtf.format(3, 'week');  // 'in 3 weeks'

Time zones

A Date is a single instant; the time zone only matters when you turn it into a human-readable form. 09:30 in Mumbai and 04:00 in London are the same instant.

Time zones are tricky. Store and send dates as ISO strings (date.toISOString()), and format them in the user’s own time zone only when displaying.

You can format for any zone with the timeZone option:

JavaScript
launch.toLocaleString('en-GB', { timeZone: 'Europe/London' });

For heavy date work — recurring events, calendars across zones — a library such as date-fns or Luxon saves a lot of effort. A new built-in API called Temporal is also arriving in browsers to replace Date with something far less quirky.

Exercise

Days until

Write daysUntil(date) that returns how many whole days remain from now until the given date. Then write addDays(date, n) that returns a new Date n days later, without changing the original.

JavaScript
function daysUntil(date) {
  // your code here
}

function addDays(date, n) {
  // your code here
}

Summary

  • new Date() is now; when you pass numbers, months are counted from 0.
  • Dates are stored as milliseconds since 1 January 1970 (UTC).
  • get…/set… methods use local time; getUTC… methods use UTC.
  • Date objects are mutable — copy before changing with set….
  • Use toLocaleDateString or Intl.DateTimeFormat to show dates to people, and ISO strings to store them.