Free beginner tutorial

JavaScript Dates: Create, Format and Compare Dates

The JavaScript Date object represents a point in time. Learn how to create dates, read their parts, format them for people and compare timestamps.

Quick answer: Create a date with new Date() for the current time or pass an ISO date string for a known date. Use Intl.DateTimeFormat to display a date for readers, and compare timestamps with getTime().

Create a date

new Date() creates an object for the current moment. You can also pass a year, month index and day. The month index starts at zero, so January is 0 and December is 11.

const now = new Date();
const launchDay = new Date(2026, 9, 23); // October 23 in local time

console.log(now);
console.log(launchDay);

For a specific instant, an ISO string that includes a time-zone marker is less ambiguous than a locale-formatted string such as "10/11/2026", which can mean different dates in different regions.

Read parts of a date

Date methods such as getFullYear(), getMonth() and getDate() read the local calendar representation. Remember that getDate() is the day of the month, while getDay() is the day of the week.

const meeting = new Date(2026, 9, 23);

console.log(meeting.getFullYear()); // 2026
console.log(meeting.getMonth());    // 9, October
console.log(meeting.getDate());     // 23
console.log(meeting.getDay());      // weekday number

Format a date for people

Use Intl.DateTimeFormat to format a date with the reader's language conventions. This is more suitable for interface text than showing a raw timestamp.

const eventDate = new Date();

const formatted = new Intl.DateTimeFormat(undefined, {
  dateStyle: "long",
  timeStyle: "short"
}).format(eventDate);

console.log(formatted);

The first argument can be a locale such as "en-GB" when a product has a specific locale requirement. If it is undefined, the browser uses the visitor's default locale.

Compare dates with timestamps

getTime() returns the number of milliseconds since the Unix epoch. Compare these numbers to sort dates or check which instant came first.

const first = new Date("2026-10-23T09:00:00Z");
const second = new Date("2026-10-23T10:00:00Z");

console.log(first.getTime() < second.getTime()); // true

Adding a fixed number of milliseconds is appropriate for measuring elapsed time. Calendar operations such as “one month later” need extra care because months have different lengths and local time can change with daylight saving rules.

Common JavaScript date questions

Why does getMonth() return a number one lower?

JavaScript numbers months from zero to eleven. Add one when displaying the number as a human month, or use Intl.DateTimeFormat to format the month name.

What does toISOString() return?

toISOString() returns a standardized date and time string in UTC, ending with Z. It is useful for exchanging timestamps, but a reader-facing date may need locale formatting.

How can I compare two JavaScript dates?

Compare their timestamps using getTime(), or compare the Date objects directly with relational operators. Timestamps make the comparison explicit.

Practice: display today's date

Create a Date for the current moment, format it with Intl.DateTimeFormat, and display the result in a page. Then change the locale and compare how the date is written.