setters
Date.prototype.setFullYear
Mutates the Date in place, setting the year (and optionally month and day) in LOCAL time, and returns the new timestamp in milliseconds. The parallel setters setMonth, setDate, setHours, setMinutes, setSeconds, setMilliseconds follow the same in-place mutation pattern.
date.setFullYear(year[, month[, date]]) Parameters
| Parameter | Purpose |
|---|---|
| year | 4-digit year |
| month | optional 0-11 month |
| date | optional 1-31 day of month |
| return value | the resulting getTime() (ms since epoch) |
Examples
const d = new Date('2026-07-19'); d.setFullYear(2030); console.log(d.getUTCFullYear()); // 2030 mutates in place
const d = new Date(2026, 0, 31); d.setMonth(1); console.log(d.toDateString()); // spills into March setMonth: Feb 31 -> Mar 3
const d = new Date('2026-07-19'); d.setDate(d.getDate() + 7); console.log(d.toISOString()); // one week later add days by overflow
const d = new Date(); d.setHours(0,0,0,0); console.log(d.toString()); // start of local day setHours accepts h, m, s, ms
Gotcha
Setters MUTATE the original Date - clone with new Date(d) first if you need to keep the old value. Overflow is silently normalized (setMonth(12) becomes January of the next year).
Related
Date.prototype.getFullYear
Returns the 4-digit year of the date according to LOCAL time. Prefer this over the deprecated getYear(), which returns year-1900.
Date.prototype.getMonth
Returns the month of the date in LOCAL time as an integer from 0 (January) to 11 (December). This zero-based indexing is the single most common source of off-by-one date bugs in JavaScript.
new Date()
Creates a Date object representing a single moment in time as milliseconds since the Unix epoch (1970-01-01T00:00:00Z). Called with no arguments it returns the current instant; with arguments it parses a timestamp, ISO string, or component parts.