JavaScript Date and Time

Create, format, and modify dates in JavaScript with timestamps, UTC, and common calculations.

TL;DR

  1. 01Create dates using new Date with several input options.
  2. 02Read date parts with getFullYear(), change them with setFullYear().
  3. 03Format dates with toISOString() for machines and toLocaleDateString() for humans.

Tips

  1. 01For complex date math like time zones, recurring events, or relative time, use a library like date-fns or Luxon.
  2. 02Store dates as ISO strings or timestamps in APIs and databases, converting to a Date object only for display.

Warnings

  1. 01JavaScript months are zero-indexed, so passing 0 means January and passing 11 means December, not month 12.
  2. 02Date string parsing varies by browser for non-ISO formats, so prefer ISO 8601 or new Date(year, month, day) instead.

Creating Dates

    new Date()

    The Date constructor accepts four different argument shapes for creating dates.

    const now = new Date();
    From milliseconds

    Pass a timestamp in milliseconds to create a date from a number.

    const fromMs = new Date(1697040000000);
    From a string

    Pass a date string to parse a specific date in standard format.

    const d = new Date("2025-10-11");
    From parts

    Pass year, month (zero-indexed), day, and time parts to build a custom date.

    const custom = new Date(2025, 9, 11, 15, 30);
    Checking validity

    Detects an invalid date by testing whether getTime() returns NaN.

    Number.isNaN(d.getTime()); // true if invalid

Reading Parts

    getFullYear()

    Reads the four-digit year directly from a Date object instance.

    now.getFullYear(); // 2025
    getMonth()

    Reads the month as a zero-indexed number, where zero means January.

    now.getMonth(); // 9 means October
    getDate() and getDay()

    getDate() returns the day of the month; getDay() returns the weekday.

    now.getDate();
    now.getDay(); // 0 = Sunday
    Time components

    Use getHours(), getMinutes(), and getSeconds() to read the time components.

    now.getHours();
    now.getMinutes();
    now.getSeconds();
    getTime()

    Reads the date as a millisecond timestamp since the Unix epoch.

    now.getTime(); // ms since epoch

Changing Parts

    setFullYear()

    Every setter mutates the original Date object in place — there's no immutable version.

    const date = new Date();
    date.setFullYear(2026);
    setMonth()

    Changes the month in place, again starting counting from zero.

    date.setMonth(0); // January
    setDate()

    Changes the day of the month, rolling over into next month.

    date.setDate(1);
    setHours()

    Use extra arguments to set minutes and seconds at the same time.

    date.setHours(12, 0, 0);
    setTime()

    Sets the entire date at once from a millisecond timestamp value.

    date.setTime(0); // Jan 1, 1970 UTC

Timestamps and UTC

    Date.now()

    Returns the current timestamp in milliseconds since the Unix epoch.

    const ts = Date.now();
    getUTC methods

    Reads times in universal coordinated time instead of the local timezone.

    now.getUTCFullYear();
    now.getUTCHours();
    Easy comparisons

    Timestamps make it easy to compare or subtract two dates directly.

    date1.getTime() < date2.getTime();
    toJSON()

    Runs automatically inside JSON.stringify(), which produces an ISO date string.

    JSON.stringify({ d: now }); // ISO string
    new Date(ts)

    Convert any millisecond timestamp back into a usable Date object.

    const d = new Date(1700000000000);

Formatting and Math

    toISOString()

    Formats a date for machines, always returning UTC in a sortable format.

    now.toISOString();
    // "2025-10-11T00:00:00.000Z"
    toLocaleDateString()

    Formats the date for the user's region for a human-readable display.

    now.toLocaleDateString();
    Adding days

    Read the current date and add to it to shift by any number of days.

    const today = new Date();
    today.setDate(today.getDate() + 5);
    Subtracting dates

    Subtract two dates to get the difference in milliseconds between them.

    const diffMs = date1 - date2; // milliseconds
    Converting units

    Convert milliseconds into days or hours using simple division for clarity.

    const hrs = ms / (1000 * 60 * 60);

In Practice

FAQ