JavaScript Date Object

The JavaScript Date object represents one instant in time. Calling new Date() without arguments creates an object for the current instant.

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

You can create dates from the current time, a timestamp, numeric date parts, or a standard date-time string.

Date Constructor Forms Top ↑

const now = new Date();
const fromMs = new Date(0);
const local = new Date(2026, 8, 10, 14, 30, 0);
const fromIso = new Date("2026-09-10T14:30:00+05:30");

The multi-argument numeric form uses local time and a zero-based month. The string form is safest when it follows the standard date-time format and explicitly includes a time-zone offset when needed.

Date Timestamps and the Unix Epoch Top ↑

Internally a Date is based on milliseconds relative to 1 January 1970 00:00:00 UTC. Use getTime() to retrieve the timestamp.

const date = new Date();
const timestamp = date.getTime();
console.log(timestamp);

Creating a Date from Numeric Parts Top ↑

const meeting = new Date(2026, 8, 10, 10, 34, 45, 500);
console.log(meeting);

The argument order is year, month, day, hours, minutes, seconds and milliseconds. Missing trailing parts default to their minimum values, while the day defaults to 1.

Month numbers start at zero: 8 means September. This is one of the most common Date mistakes.

Creating Dates from Strings Top ↑

const withOffset = new Date("2026-09-10T14:30:00+05:30");
const utc = new Date("2026-09-10T09:00:00Z");

Avoid depending on ambiguous, locale-specific strings such as 09/10/2026. If the instant matters, include Z or an explicit offset such as +05:30.

Local Time vs UTC Top ↑

The Date object itself represents the instant; the getter determines how it is interpreted. Compare local and UTC hours:

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

See UTC Date methods for the corresponding UTC getters and time-zone offset behavior.

Detecting an Invalid Date Top ↑

const date = new Date("not-a-date");
if (Number.isNaN(date.getTime())) {
  console.log("Invalid date");
}

Comparing and Copying Dates Top ↑

Compare timestamps rather than comparing two Date objects with ===.

const a = new Date("2026-09-10T10:00:00Z");
const b = new Date("2026-09-10T11:00:00Z");
console.log(a.getTime() < b.getTime());
const copy = new Date(a.getTime());

Displaying a Date on the Page Top ↑

The original page used an alert and inline click handler. For a page interface, an event listener and textContent are easier to maintain.

const button = document.querySelector("#show-time");
const output = document.querySelector("#time-output");

button.addEventListener("click", () => {
  output.textContent = new Date().toString();
});

For a continuously updating display see the JavaScript clock, and for an analog version see the HTML canvas clock.

Analog clock built with HTML canvas and JavaScript

Common Date Object Mistakes Top ↑

  • Using 1 for January in the numeric constructor instead of 0.
  • Parsing ambiguous date strings.
  • Assuming the Date object stores a named time zone.
  • Comparing Date objects with === instead of their timestamps.
  • Forgetting to validate user-supplied dates.

Date Reference getTime()




Subscribe to our YouTube Channel here



plus2net.com







01-06-2020

hi, how do I move the button to the middle of the page?

09-06-2020

Align Div



✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer