JavaScript Dates
The built-in Date object represents a single moment in time. Internally it's just a number: the milliseconds since 1 January 1970, 00:00:00 UTC (the "Unix epoch"). Methods on top of that number let you read and change the year, month, day, hour and so on.
Note: All examples in this chapter create fixed dates and print them in UTC, so you get the same output as shown. In a real app,
new Date()with no arguments gives the current date and time, and methods withoutUTCin their name use the user's local time zone.
Creating dates
const fromIso = new Date("2025-03-14T10:00:00Z"); // ISO 8601 string, Z = UTC
const fromMillis = new Date(0); // the epoch
const fromParts = new Date(Date.UTC(2024, 11, 31, 23, 59)); // month 11 = December!
console.log(fromIso.toISOString());
console.log(fromMillis.toISOString());
console.log(fromParts.toISOString());
console.log(fromIso.getTime()); // milliseconds since epoch2025-03-14T10:00:00.000Z
1970-01-01T00:00:00.000Z
2024-12-31T23:59:00.000Z
1741946400000
| Way to create | Meaning |
|---|---|
new Date() |
now (non-deterministic — not used in our examples) |
new Date("2025-03-14T10:00:00Z") |
parse an ISO string (always include Z or an offset) |
new Date(ms) |
milliseconds since the epoch |
new Date(y, m, d, h, min) |
parts in local time |
new Date(Date.UTC(y, m, d, h, min)) |
parts in UTC |
Warning: Months are zero-based: January is
0, December is11. Days of the month start at1. This trips up everyone at least once.
Reading parts of a date
Each getter comes in a local version (getHours) and a UTC version (getUTCHours). We use UTC here so the output doesn't depend on your time zone:
const d = new Date("2025-03-14T10:30:45Z");
const days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
console.log(d.getUTCFullYear());
console.log(d.getUTCMonth()); // 2 = March
console.log(d.getUTCDate()); // day of the month
console.log(days[d.getUTCDay()]); // day of the week, 0 = Sunday
console.log(d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());2025
2
14
Fri
10 30 45
Changing dates and doing date arithmetic
Setters such as setUTCDate change a date in place and handle overflow automatically — adding 20 days to 14 March correctly lands in April. To find the difference between two dates, subtract them: you get milliseconds.
const start = new Date("2025-03-14T00:00:00Z");
const due = new Date(start); // copy first!
due.setUTCDate(due.getUTCDate() + 20);
console.log(due.toISOString().slice(0, 10));
const endOfMonth = new Date(Date.UTC(2025, 2 + 1, 0)); // day 0 = last day of previous month
console.log(endOfMonth.toISOString().slice(0, 10));
const DAY = 24 * 60 * 60 * 1000;
const launch = new Date("2025-12-25T00:00:00Z");
console.log(Math.round((launch - start) / DAY), "days until launch");
console.log(start < launch);2025-04-03
2025-03-31
286 days until launch
true
Formatting dates
toISOString() gives a precise, machine-friendly string. For people, use toLocaleDateString or Intl.DateTimeFormat with an explicit locale and time zone:
const d = new Date("2025-03-14T10:00:00Z");
console.log(d.toISOString());
console.log(d.toISOString().slice(0, 10));
console.log(d.toLocaleDateString("en-US", { timeZone: "UTC" }));
console.log(d.toLocaleDateString("de-DE", { timeZone: "UTC" }));
console.log(
d.toLocaleDateString("en-US", {
timeZone: "UTC",
weekday: "long",
day: "numeric",
month: "long",
year: "numeric",
})
);
const pad = (n) => String(n).padStart(2, "0");
console.log(`${pad(d.getUTCDate())}/${pad(d.getUTCMonth() + 1)}/${d.getUTCFullYear()}`);2025-03-14T10:00:00.000Z
2025-03-14
3/14/2025
14.3.2025
Friday, March 14, 2025
14/03/2025
Things to watch out for
- Parsing: only ISO 8601 strings (
"2025-03-14"or"2025-03-14T10:00:00Z") parse reliably everywhere. Formats like"03/14/2025"vary between browsers. - Date-only strings such as
"2025-03-14"are treated as UTC, while date-time strings withoutZ("2025-03-14T10:00") are treated as local time. - Mutability: setters change the object; copy with
new Date(d)before modifying. - Invalid dates:
new Date("nonsense")doesn't throw — itsgetTime()isNaN. Check withNumber.isNaN(d.getTime()).
The current time is read like this — fine in your own apps, but its output changes every time:
const now = new Date();
console.log(now.toISOString());
console.log(Date.now()); // milliseconds since the epoch, as a numberTest yourself
Which month does Date.UTC(2025, 0, 1) refer to?
- December 2024
- January 2025
- February 2025
- It is invalid
What do you get when you subtract one Date from another?
- A new Date
- The difference in days
- The difference in milliseconds
- NaN
What does getUTCDay() return?
- Day of the month (1–31)
- Day of the week (0 = Sunday)
- Day of the year
- Number of days since 1970