Dates
Getting & formatting, Intl formatting 🟢, Temporal (the future) 🟢
Date is famously awkward; know its traps and reach for the modern option when possible.
new Date(); // now
new Date("2025-01-31T10:00:00Z"); // ISO parse (safe)
new Date(2025, 0, 31); // ⚠️ month is 0-indexed! → January
Date.now(); // ms since epoch (number)
Getting & formatting#
const d = new Date();
d.getFullYear(); d.getMonth() /*0-11*/; d.getDate() /*1-31*/;
d.getDay(); // 0=Sunday
d.getHours(); d.getMinutes();
d.getTime(); // epoch ms
d.toISOString(); // "2025-01-31T10:00:00.000Z" (UTC)
⚠️ Traps: months are 0-indexed; getDay is weekday not date; parsing non-ISO strings is implementation-defined; Date is mutable.
Intl formatting 🟢#
new Intl.DateTimeFormat("en-IN", {
dateStyle: "long", timeStyle: "short", timeZone: "Asia/Kolkata"
}).format(new Date());
// e.g. "31 January 2025 at 3:30 pm"
new Intl.RelativeTimeFormat("en").format(-1, "day"); // "yesterday"
new Intl.NumberFormat("en-IN", { style: "currency", currency: "INR" }).format(1234.5);
// "₹1,234.50"
Temporal (the future) 🟢#
The Temporal proposal fixes Date's design: immutable objects, explicit time zones, no 0-indexed months.
// Temporal.Now.plainDateISO(); → 2025-01-31
// Temporal.PlainDate.from("2025-01-31").add({ days: 5 });
Not yet universal at time of writing — use a polyfill, or a library (Luxon, date-fns, Day.js) for production date math today.
Interview Q&A#
Q1. Why is new Date(2025, 1, 1) February, not January?
The month argument in the numeric Date constructor is 0-indexed, so 0 is January and 1 is February. Only the month is 0-based — the day argument is 1-based, which makes it an easy trap.
Q2. How do you format a date for a specific locale and time zone?
Use Intl.DateTimeFormat with a locale and options including timeZone, e.g. new Intl.DateTimeFormat("en-IN", { dateStyle: "long", timeStyle: "short", timeZone: "Asia/Kolkata" }).format(new Date()). It handles locale-aware ordering, month names, and zone conversion without manual math.
Q3. What does Temporal improve over Date?
Temporal offers immutable objects (no accidental mutation), first-class explicit time-zone and calendar support, non-ambiguous parsing, and 1-indexed months — fixing Date's core design flaws. Until it's universal, use a polyfill or a library like Luxon or date-fns for production date math.