JavaScript Tutorial · JavaScript Toolbox · Lesson 36 of 44
JavaScript Dates
Create dates, read their parts, do date maths and format them.
Creating dates
new Date() is the current moment. You can also build a date from parts or from an ISO string:
const d = new Date(2026, 0, 15, 9, 30); // 15 Jan 2026, 09:30 local time
console.log(d.getFullYear(), d.getMonth(), d.getDate());
console.log(d.getDay()); // 0 = Sunday … 6 = Saturday
console.log(d.getHours(), d.getMinutes());
const iso = new Date("2026-03-01T12:00:00Z");
console.log(iso.toISOString());
Output
2026 0 15 4 9 30 2026-03-01T12:00:00.000Z
Watch out: months are zero-based — January is 0, December is 11.
Date arithmetic
Dates are stored as milliseconds since 1 January 1970 (UTC). Subtract dates to get a difference in milliseconds:
const start = new Date("2026-01-01T00:00:00Z");
const end = new Date("2026-03-01T00:00:00Z");
const days = (end - start) / (1000 * 60 * 60 * 24);
console.log(days);
const due = new Date(start);
due.setUTCDate(due.getUTCDate() + 45);
console.log(due.toISOString().slice(0, 10));
Output
59 2026-02-15
Formatting
toISOString() gives a machine-friendly UTC string. For people, use Intl.DateTimeFormat or toLocaleDateString() with a locale and options:
const launch = new Date(Date.UTC(2026, 6, 4, 18, 0));
const opts = { dateStyle: "full", timeStyle: "short", timeZone: "UTC" };
console.log(new Intl.DateTimeFormat("en-US", opts).format(launch));
console.log(launch.toLocaleDateString("en-GB", { timeZone: "UTC" }));
console.log(launch.toISOString());
Output
Saturday, July 4, 2026 at 6:00 PM 04/07/2026 2026-07-04T18:00:00.000Z
The built-in Date is famously awkward. The new Temporal API fixes it; until it's everywhere, libraries like date-fns and dayjs are popular.
Test yourself
What does new Date(2026, 1, 1).getMonth() return?
01212
Show answer
Answer: 1
Months are zero-based, so month 1 is February and getMonth() returns 1.