JavaScript setDate(): Set, Add and Subtract Days

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.

Setting the Day of the Month Top ↑

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

Using Values Outside the Current Month Top ↑

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

Adding or Subtracting Days Top ↑

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.

Return Value and Mutation Top ↑

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

Keeping the Original Date Unchanged Top ↑

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);

Local Time, UTC and Daylight-Saving Changes Top ↑

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);

Common setDate() Mistakes Top ↑

  • Expecting a new Date object: the method mutates the existing object.
  • Using setDate(365) when the real intention is “add one year.” Use setFullYear() when year semantics matter.
  • Assuming every local calendar day is exactly 86,400,000 ms across DST changes.
  • Passing invalid data such as NaN, which makes the Date invalid.

Next: setMonth() Date Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer