Date.prototype.setDate() changes the day of the month on an existing JavaScript Date. It uses local time, mutates the Date object, automatically normalizes values outside the normal day range, and returns the updated timestamp in milliseconds.
Syntax:
date.setDate(dayOfMonth);
For example, this sets a date to the 15th day of its current month:
const date = new Date(2026, 8, 10);
date.setDate(15);
console.log(date.toDateString()); // Tue Sep 15 2026
The method is especially useful for day-based date arithmetic because values such as 0, 35, or negative numbers roll into the previous or next month automatically.
The argument is an integer representing the day number. Valid calendar days depend on the month, but JavaScript does not restrict the argument to 1–31 because overflow and underflow are intentionally normalized.
const date = new Date(2026, 8, 10);
date.setDate(1);
console.log(date.toDateString()); // Tue Sep 01 2026
The original Plus2net example used 35. That behavior is valid and useful: JavaScript starts from the current month and carries the extra days into the next month.
const date = new Date(2026, 8, 1);
date.setDate(35);
console.log(date.toDateString()); // Mon Oct 05 2026
setDate(0) means the last day of the previous month. Negative values move farther backward from that boundary.
const date = new Date(2026, 8, 10);
date.setDate(0);
console.log(date.toDateString()); // Mon Aug 31 2026
To add days to the current date, read the current day with getDate() and pass the adjusted value back to setDate(). This works across month and year boundaries.
const date = new Date(2026, 11, 28);
date.setDate(date.getDate() + 7);
console.log(date.toDateString()); // Mon Jan 04 2027
Subtract days by using a negative adjustment:
const date = new Date();
date.setDate(date.getDate() - 20);
console.log(date);
The original page also demonstrated setDate(365). JavaScript permits it, but for “one year later” semantics, setFullYear() expresses the intent more clearly.
setDate() changes the original object and returns its new timestamp. It does not return a new Date.
const date = new Date("2026-09-10T10:00:00");
const result = date.setDate(20);
console.log(typeof result); // "number"
console.log(date.getDate()); // 20
If other code still needs the original Date, clone it before applying setters.
const original = new Date();
const changed = new Date(original);
changed.setDate(changed.getDate() + 7);
console.log(original);
console.log(changed);
setDate() works in local time. Crossing a daylight-saving transition can make the elapsed milliseconds differ from exactly 24 hours per calendar day. When the calculation must follow UTC calendar days, use setUTCDate()/getUTCDate(); when you need an exact elapsed duration, operate on timestamps instead.
const date = new Date();
date.setUTCDate(date.getUTCDate() + 1);
setDate(365) when the real intention is “add one year.” Use setFullYear() when year semantics matter.NaN, which makes the Date invalid.Next: setMonth() Date Reference
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.