Dates
Overview
The Date object represents a single moment in time.
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 UTCDates 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.
launch.getTime(); // e.g. 1798137000000 (depends on your time zone)
Date.now(); // milliseconds right now — handy for timing thingsCreating 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:
const bad = new Date('not a date');
Number.isNaN(bad.getTime()); // true — check before using itReading parts
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(); // 0These 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:
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:
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 FebruaryDates are mutable.
setDatechanges the Date object itself. Copy it first withnew 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:
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(); // trueFormatting
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:
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 formatRelative times
Intl.RelativeTimeFormat produces phrases like “in 3 days” or “2 hours ago” in any language:
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:
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.
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.
function daysUntil(date) {
// your code here
}
function addDays(date, n) {
// your code here
}function daysUntil(date) {
const ms = date.getTime() - Date.now();
return Math.ceil(ms / (1000 * 60 * 60 * 24));
}
function addDays(date, n) {
const copy = new Date(date);
copy.setDate(copy.getDate() + n);
return copy;
}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
toLocaleDateStringorIntl.DateTimeFormatto show dates to people, and ISO strings to store them.