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

JS Date Set Methods

Set methods change a Date in place. Each set… normalises overflow — useful for "add one day" math without breaking month boundaries.

The setters

MethodSets
setFullYear(y, [m], [d])Year (and optionally month/day).
setMonth(m, [d])Month (0–11) and optionally day.
setDate(d)Day of month.
setHours / Minutes / Seconds / Milliseconds(…)Time pieces.
setTime(ms)Whole instant in one call (epoch ms).

Automatic overflow

JS
const d = new Date(2026, 0, 31);   // 2026-01-31

d.setDate(d.getDate() + 1);
d.toDateString();                  // "Sun Feb 01 2026" — wraps automatically

d.setMonth(13);                    // → next year February
d.setHours(-1);                    // → previous day, 23:00

Common patterns

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

// Start of the day
const startOfDay = new Date();
startOfDay.setHours(0, 0, 0, 0);

// First day of next month
const nextMonth = new Date();
nextMonth.setMonth(nextMonth.getMonth() + 1, 1);

// Build from parts (UTC, to avoid local-time surprises)
const utc = new Date(Date.UTC(2026, 5, 6, 12));     // 2026-06-06 12:00 UTC

Immutable equivalents

If you don't want to mutate, copy first:

JS
const future = new Date(now);
future.setDate(now.getDate() + 30);
Tip: For complex date math (timezones, durations, calendar arithmetic), use the upcoming Temporal API or a library like date-fns. Mutating Date in place is a footgun in async code.

Example

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

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

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

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

Exercise

Add one day to a Date in place.

d.setDate(d. () + 1);

Test yourself

Q1. `d.setDate(32)` does…
Q2. Setters mutate the Date…
Q3. Build a UTC date precisely with…

Discussion

Loading…