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.
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.
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);
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.
8 means September. This is one of the most common Date mistakes.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.
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.
const date = new Date("not-a-date");
if (Number.isNaN(date.getTime())) {
console.log("Invalid date");
}
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());
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.
=== instead of their timestamps.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
01-06-2020 | |
| hi, how do I move the button to the middle of the page? | |
09-06-2020 | |
| Align Div | |