R
Rishtaara
JavaScript Fundamentals
Lesson 10 of 28Article14 min

Date Object & Temporal API

Use new Date() for now, new Date("2026-07-24"), or new Date(year, monthIndex, day). Month index starts at 0 — January is 0.

Creating Dates

Use new Date() for now, new Date("2026-07-24"), or new Date(year, monthIndex, day). Month index starts at 0 — January is 0.

Real-life example: new Date() is looking at today's calendar on the wall right now.

New Date examples
const now = new Date();
const fixed = new Date("2026-07-24");
const parts = new Date(2026, 6, 24); // month 6 = July

Getting Date Parts

Methods like getFullYear(), getMonth(), getDate(), getDay(), getHours() read parts of a date.

Real-life example: Reading day, month, year from a paper calendar one field at a time.

Get methods
const d = new Date();
d.getFullYear();  // e.g. 2026
d.getMonth();     // 0–11
d.getDate();      // day of month
d.getDay();       // 0=Sunday … 6=Saturday

Formatting Dates

toDateString(), toISOString(), and toLocaleDateString() turn a Date into readable text.

Real-life example: Same birthday written as "24 Jul 2026" or "2026-07-24" — different formats, same day.

Format output
const d = new Date("2026-07-24");
d.toDateString();                    // "Fri Jul 24 2026"
d.toISOString();                     // UTC string
d.toLocaleDateString("en-IN");       // Indian format

Temporal API — what and why

The Temporal API is a modern replacement for Date. It fixes timezone bugs and makes date math clearer (PlainDate, ZonedDateTime).

Browser support is still growing — check Can I Use before production. Learn Date first; Temporal is the future.

Real-life example: Old Date is a broken wall clock with no city label. Temporal is a clock that knows Delhi vs London time clearly.

Tip: Store dates in ISO format (2026-07-24) in databases. Convert to local text only when showing users on Rishtaara.
Temporal concept (when supported)
// Future-friendly style (needs Temporal in browser)
// const today = Temporal.Now.plainDateISO();
// const meeting = Temporal.PlainDate.from("2026-07-24");
// today.until(meeting).days; // days until meeting

// For now, Date + toLocaleString works everywhere:
const event = new Date("2026-07-24T10:00:00");
console.log(event.toLocaleString("en-IN"));