iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JS Dates

JavaScript's built-in Date object represents a moment in time as the number of milliseconds since 1970-01-01 UTC. Its API is awkward — there's a reason libraries like date-fns and the new Temporal proposal exist.

Creating dates

JS
new Date();                           // now
new Date("2026-06-06T12:00:00Z");     // ISO 8601 (recommended)
new Date(2026, 5, 6, 12, 0, 0);       // year, month (0-indexed!), day, h, m, s
new Date(1748102400000);              // milliseconds since epoch

Date.now();                           // current epoch ms — no allocation

Most-used getters

MethodReturns
getFullYear()4-digit year
getMonth()0–11 (January is 0!)
getDate()Day of month, 1–31
getDay()Day of week, 0 (Sun) – 6 (Sat)
getHours / Minutes / Seconds / MillisecondsTime parts
getTime()Epoch ms
toISOString()"2026-06-06T12:00:00.000Z"
toLocaleDateString(locale, opts)Human-readable, locale-aware

Common operations

JS
// Tomorrow
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

// Days between
const days = Math.round((later - earlier) / 86400000);

// Format
new Date().toLocaleDateString("en-US", {
  year: "numeric", month: "long", day: "numeric",
});   // "June 6, 2026"
Gotcha: Months are 0-indexed (January is 0) but days are 1-indexed. It's the single most reported beginner bug in JS.
Tip: For anything complex (timezone math, parsing, intervals), use a library or the new Temporal API. The legacy Date mixes UTC and local in awkward ways.

Example

Example
<!DOCTYPE html>
<html>
<body>

<p id="out"></p>

<script>
document.getElementById("out").textContent = "Hello from JS Dates!";
</script>

</body>
</html>
Try it Yourself »

Exercise

Get the current time in milliseconds.

const now = Date. ();

Test yourself

Q1. JS month numbering starts at…
Q2. Get the current epoch milliseconds with…
Q3. ISO 8601 format is…

Discussion

Loading…