Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Class

Date

The JavaScript Date class represents a single instant as milliseconds from the UTC epoch. It offers constructors, static timestamp utilities, and local or UTC accessors and formatters.

How it works

A Date stores one instant as an integral millisecond timestamp relative to 1970-01-01T00:00:00Z. Its accessors and mutators can interpret or change that instant in either local time or UTC, while formatting methods produce strings.

Syntax

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)
Date()

Returns

A Date object represents an instant in time; the Date() function form returns a current date-time string.

Key points

  • A Date models one point on the timeline rather than a time-zone-aware calendar date.
  • Its core value is an integer count of elapsed milliseconds from the UTC epoch.
  • getTime() reads the timestamp, while setTime() replaces it and changes the represented instant.
  • Date.now() provides the current epoch timestamp without creating a Date object.
  • Date.parse() converts a date string into an epoch-millisecond number.
  • valueOf() and numeric coercion expose the Date's timestamp, including negative values for instants before the epoch.

Common mistakes

  • Calling Date() without new returns the current date-time string rather than constructing a Date object.
  • Values outside the supported timestamp range produce an Invalid Date whose numeric value is NaN.
  • The stored timestamp does not contain a time zone; local and UTC methods are different views of the same instant.

Example: Create and inspect Date values

const epoch = new Date(0);
const now = new Date();

console.log(epoch.toISOString());
console.log(now.getTime());
console.log(Date.now());

Creates dates, formats the epoch in UTC, and reads current timestamps.

When to use it

Timestamps

Measure elapsed time or store event instants using epoch milliseconds.

Local calendar operations

Read calendar fields according to the runtime's local time zone.

UTC processing

Read or format an instant consistently in UTC.

Date parsing

Convert supported date strings into timestamps.

Interview questions

QWhat does a JavaScript Date store?

A Date represents one instant using milliseconds from the UTC epoch, 1970-01-01T00:00:00Z. The timestamp itself is independent of time zone.

Follow-up: How do you detect an invalid Date? Use Number.isNaN(date.getTime()), because invalid dates have a NaN timestamp.

QHow do Date() and new Date() differ?

new Date() constructs a Date object for the current instant. Date() without new instead returns a string for the current date and time, and supplied arguments to the function form are ignored.

Follow-up: What does new Date(0) represent? It represents the epoch instant, midnight at the beginning of January 1, 1970 UTC.

QHow do local and UTC Date methods differ?

Methods beginning with getUTC interpret calendar fields in universal time, while methods such as getDate() interpret them in local time. Both operate on the same underlying timestamp.

Follow-up: Why can local calendar fields differ from UTC fields? The host environment's local time zone may place the instant on a different date or clock time.

  • Date.now()
  • Date.parse()
  • Date.UTC()
  • Date.prototype.getTime()
  • Date.prototype.setTime()
  • Date.prototype.toISOString()
  • Date.prototype.getUTCFullYear()

Source: Date - JavaScript | MDN. Checked 2026-10-15.