Secure & Private (Zero Data Retention)
Free Access • No Sign-Up
Working with Dates
Dates and times are common in web applications. Learn how to format and manipulate dates reliably.
Try It Yourself
📋 Copy Intl.DateTimeFormat & Safe Date Math Snippet
// Modern Intl.DateTimeFormat & Timestamp Calculations
const now = new Date();
// High-Precision Localized Formatting
const formatter = new Intl.DateTimeFormat("en-US", {
dateStyle: "full",
timeStyle: "short",
timeZone: "America/New_York"
});
console.log(formatter.format(now));
// Date Arithmetic via Timestamps
const tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000);
⚠️ 5 Fatal Traps & JavaScript Pitfalls
Trap #1: 0-Indexed Months Trap
In JavaScript Date, months are 0-indexed (January is 0, December is 11). Writing new Date(2026, 1, 1) creates February 1st, NOT January 1st!
Trap #2: String Date Parsing Ambiguity
Parsing strings like new Date("05/06/2026") produces May 6 in the US and June 5 in Europe. Always use ISO 8601 strings: "YYYY-MM-DDTHH:mm:ssZ".
Trap #3: In-Place Mutation with Date Setters
Calling date.setDate(date.getDate() + 7) mutates the original Date instance in-place. Clone before modifying: new Date(date.getTime()).
Trap #4: Comparing Dates with Loose or Strict Equality
dateA === dateB compares object memory references, returning false even if both dates represent the exact same millisecond. Compare with dateA.getTime() === dateB.getTime().
Trap #5: Daylight Saving Time (DST) Arithmetic Offsets
Adding 86,400,000 milliseconds (24 hours) across a DST transition can shift clock times by 1 hour. Use calendar setters or modern Temporal APIs.
💬 Frequently Asked Questions
What is the modern alternative to the legacy JavaScript Date object?
The upcoming TC39 Temporal API provides immutable, timezone-aware, ISO-compliant date and time objects designed to replace Date.
How do you format dates without third-party libraries in modern browsers?
Use the native Intl.DateTimeFormat API, which formats dates and times according to locale standards and specific time zones with zero bundle overhead.
What is the Unix epoch in JavaScript?
The Unix epoch is midnight on January 1, 1970, UTC. Date.now() returns milliseconds elapsed since this epoch.
How do you calculate the difference in days between two dates?
Subtract their timestamps and divide by milliseconds per day: Math.floor((dateB - dateA) / (1000 * 60 * 60 * 24)).
What does date.toISOString() return?
It returns a standardized ISO 8601 string representation in UTC ending with "Z" (e.g. 2026-09-06T11:00:00.000Z).