new Date()
Creates a Date for the current instant.
Date represents a moment in time as milliseconds since the Unix epoch. It is useful, but it has old design choices that require care.
const now = new Date(); const timestamp = now.getTime(); console.log(now.toISOString()); console.log(timestamp);
Dates, Time & Intl
A Date object represents one exact instant in time. Internally, that instant is stored as a number of milliseconds since 1970-01-01T00:00:00.000Z.
Date can show that same instant in local time or UTC. This is useful, but it also creates confusion because the same object can appear different depending on which method you call.
Date is old and widely supported. Learn it well, but treat parsing, month numbers, mutation and time zones with extra attention.
Creates a Date for the current instant.
getTime returns milliseconds since the Unix epoch.
toISOString returns a stable ISO string in UTC.
Failed parsing can create a Date whose time value is NaN.
Examples
const date = new Date("2026-01-01T09:30:00Z");
if (!Number.isNaN(date.getTime())) {
console.log(date.toISOString());
}
const date = new Date("01/02/2026");
// This can be read differently by people and systems.
Code patterns
Dates and time zones become much easier when every example names the concept it represents: instant, local display, UTC output, calendar date, formatted text or zoned time.
Use new Date() when you need the current moment.
const now = new Date(); console.log(now.toISOString());
A timestamp is milliseconds since the Unix epoch.
const timestamp = 1767225600000; const date = new Date(timestamp); console.log(date.toISOString());
ISO strings are the safest Date parsing input.
const date = new Date("2026-01-01T09:30:00Z");
console.log(date.toISOString());
Check the internal time value before using it.
const date = new Date("not a date");
const isValid = !Number.isNaN(date.getTime());
console.log(isValid);
Rules that matter
Date-time bugs usually start when storage format, calculation logic and user-facing display are treated as the same thing. Keep those responsibilities separate and the code becomes much easier to trust.
ISO date-time strings are clearer than regional text formats.
Invalid Date objects still exist, but their time value is NaN.
Display can change by time zone, but the instant is the same.
Comparing timestamps is clearer than comparing formatted strings.
Do not rely on regional date strings such as 01/02/2026.
Months are zero-based in Date constructors and getters.
Production thinking
Date bugs often appear only for some users, some time zones or some date formats. Stable input and validation prevent quiet failures.
Dates shown to users should be readable, complete and not rely only on visual order such as 01/02/2026.
Production code should store stable timestamps or ISO values, validate input and format display at the edge.
Published dates, updated dates and event dates should be stable, machine-readable and consistently formatted.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Dates are full of traps. Explain the concept first, then run the code and check whether the output matches your mental model.