๐Ÿ“… JavaScript

JavaScript Dates Without Tears โ€” Formatting, Math & Intl

๐Ÿ“… Jul 4, 2026 โฑ 3 min read

Dates are where confident developers get humbled. The essentials plus the traps:

Creating and reading

const now = new Date();
const d = new Date("2026-07-04");        // ISO string โ€” the safe format
const d2 = new Date(2026, 6, 4);         // โš ๏ธ month is 0-INDEXED: 6 = July!

d.getFullYear()  // 2026
d.getMonth()     // 6  โš ๏ธ +1 for humans
d.getDate()      // 4  (day of month)
d.getDay()       // 6  (weekday, 0=Sunday)
Date.now()       // timestamp ms โ€” for measuring/IDs

Formatting โ€” stop concatenating, use Intl

new Intl.DateTimeFormat("en-IN", { dateStyle: "medium" }).format(d)
// "4 Jul 2026" โ€” locale-correct, zero libraries

d.toLocaleDateString("en-IN", { weekday: "long", day: "numeric", month: "long" })
// "Saturday, 4 July 2026"

new Intl.RelativeTimeFormat("en").format(-2, "day")   // "2 days ago"

Date math

const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);      // handles month/year rollover!

const days = Math.round((examDate - now) / 86400000);   // countdown

Traps: getMonth() zero-indexing, timezone shifts when parsing non-ISO strings, and mutating setters. For heavy date work, the upcoming Temporal API fixes all of it โ€” until then, ISO strings + Intl.

โ† All Articles