Learn JavaScript
❯ types.date

Dates & Times with Date in JavaScript

$concept

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.

$how it works
1

Timestamps first

Date.now() returns ms since epoch — arithmetic-safe numbers for intervals and expirations.

2

Months are 0-based

new Date(2026, 8, 20) is September — the eternal off-by-one; days of month start at 1 normally.

3

Parse strictly

Only ISO strings parse reliably cross-engine — new Date("2026-09-20") yes, "09/20/2026" maybe.

4

Store UTC, show local

toISOString for storage/transmission; toLocaleString/DateFormat for humans — never mix the jobs.

$example
let d = new Date(2026, 8, 20);
console.log(d.getFullYear(), d.getMonth());
console.log(Date.now() > 0);
Output
2026 8
true
1Month 8 constructs September — zero-based as documented.
2getFullYear reads 2026 (never getYear); getMonth reads back the raw 8.
3Date.now() returns a positive timestamp — true confirms a live clock.
$use cases
Timestamps and expirations as epoch numbers
Human dates via toLocaleString/Intl (never manual math)
Interval measurement with timestamp subtraction
ISO-only parsing plus constructed dates elsewhere

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.