TExcelTAD
Home › JavaScript › JS Dates

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 without UTC in their name use the user's local time zone.

Creating dates

js · try it yourself
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 epoch
2025-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 is 11. Days of the month start at 1. 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:

js · try it yourself
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.

js · try it yourself
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:

js · try it yourself
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 without Z ("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 — its getTime() is NaN. Check with Number.isNaN(d.getTime()).

The current time is read like this — fine in your own apps, but its output changes every time:

js
const now = new Date();
console.log(now.toISOString());
console.log(Date.now());   // milliseconds since the epoch, as a number

Test yourself

Which month does Date.UTC(2025, 0, 1) refer to?

  1. December 2024
  2. January 2025
  3. February 2025
  4. It is invalid

What do you get when you subtract one Date from another?

  1. A new Date
  2. The difference in days
  3. The difference in milliseconds
  4. NaN

What does getUTCDay() return?

  1. Day of the month (1–31)
  2. Day of the week (0 = Sunday)
  3. Day of the year
  4. Number of days since 1970