JS Dates

The Date object allows you to work with dates and times. Learn how to create and format dates.

On this page

JS Dates

The Date object is used to work with dates and times in JavaScript.

const now = new Date();
console.log(now);

You can also create specific dates.

const d1 = new Date("2025-01-01");
const d2 = new Date(2025, 0, 1); // Month is 0-based (0 = January)

console.log(d1);
console.log(d2);

JS Date Formats

JavaScript supports ISO format (recommended) and local string formats.

const date = new Date();

console.log(date.toISOString());
console.log(date.toDateString());
console.log(date.toTimeString());

ISO format (YYYY-MM-DD) is safest when parsing dates.

const isoDate = new Date("2025-03-10");
console.log(isoDate);

Avoid ambiguous formats like 03/10/2025 because they depend on locale.

JS Date Get

You can retrieve parts of a date using get methods.

const d = new Date();

console.log(d.getFullYear());
console.log(d.getMonth());     // 0-11
console.log(d.getDate());
console.log(d.getDay());       // 0-6
console.log(d.getHours());
console.log(d.getMinutes());
console.log(d.getSeconds());

Use UTC versions when working with international time.

console.log(d.getUTCFullYear());
console.log(d.getUTCMonth());

JS Date Set

You can modify parts of a date using set methods.

const date2 = new Date();

date2.setFullYear(2030);
date2.setMonth(5);
date2.setDate(15);

console.log(date2);

Timestamps

A timestamp represents milliseconds since January 1, 1970 (Unix Epoch).

const timestamp = Date.now();
console.log(timestamp);

const fromTimestamp = new Date(timestamp);
console.log(fromTimestamp);

JS Date Methods

Commonly used Date methods include:

const dt = new Date();

console.log(dt.toLocaleDateString());
console.log(dt.toLocaleTimeString());
console.log(dt.toUTCString());
console.log(dt.valueOf());

UTC vs Local Time

JavaScript stores dates internally in UTC, but displays them in local time by default.

const example = new Date("2025-03-10T10:00:00Z");
console.log(example.toString());
console.log(example.toUTCString());

Modern Formatting with Intl

Use Intl.DateTimeFormat for proper localization.

const formatter = new Intl.DateTimeFormat("en-US", {
  year: "numeric",
  month: "long",
  day: "2-digit"
});

console.log(formatter.format(new Date()));

Important Note

The built-in Date API has limitations. For complex time-zone handling, consider modern libraries or the upcoming Temporal API.

Next Step

Continue with JS Arrays to work with collections of data.

JS Dates Examples (8)