Dates & Times with Date in JavaScript
Date wraps millisecond timestamps: new Date() captures now, Date.now() returns the number directly, and getters (getFullYear/getMonth 0-based/getDate) read fields while toISOString/toLocaleString render. Arithmetic on timestamps (end - start) measures intervals; libraries (date-fns) or Intl handle serious formatting.
Gotchas cluster thickly: months are 0-based (January is 0), years need getFullYear (getYear is legacy offset), parsing non-ISO strings varies by engine (construct from parts instead), and timezones shift everything — store UTC (toISOString), display local.
Timestamps first
Date.now() returns ms since epoch — arithmetic-safe numbers for intervals and expirations.
Months are 0-based
new Date(2026, 8, 20) is September — the eternal off-by-one; days of month start at 1 normally.
Parse strictly
Only ISO strings parse reliably cross-engine — new Date("2026-09-20") yes, "09/20/2026" maybe.
Store UTC, show local
toISOString for storage/transmission; toLocaleString/DateFormat for humans — never mix the jobs.
let d = new Date(2026, 8, 20);console.log(d.getFullYear(), d.getMonth());console.log(Date.now() > 0);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.