Set methods change part of an existing Date: move it to a different day, change the hour, jump ahead a month. They are also the easiest way to do date math, because JavaScript handles month lengths and leap years for you.
| setFullYear(y, [m], [d]) | Year (and optionally month and day) |
| setMonth(m, [d]) | Month, 0 to 11 |
| setDate(d) | Day of the month |
| setHours(h, [m], [s], [ms]) | Hour (and optionally smaller units) |
| setMinutes / setSeconds / setMilliseconds | Smaller units |
| setTime(ms) | Replace the whole date with a timestamp |
const d = new Date(2026, 9, 2); // Oct 2, 2026
d.setFullYear(2027);
d.setHours(9, 15); // 9:15 AM
console.log(d.toString());
Unlike string methods, which return a new string, set methods modify the Date object itself. That matters when two variables point to the same Date:
const start = new Date(2026, 9, 2);
const end = start; // NOT a copy: same object
end.setDate(10);
console.log(start.getDate()); // 10 (start changed too!)
Make a real copy first with new Date(original):
const start = new Date(2026, 9, 2);
const end = new Date(start); // independent copy
end.setDate(10);
console.log(start.getDate(), end.getDate()); // 2 10
const does not protect a Date from set methods, just as it does not protect an object's properties. const only stops the variable being pointed at a different Date.
Several set methods accept extra, optional values. setFullYear can set the month and day at the same time:
const d = new Date("January 01, 2025");
d.setFullYear(2020, 11, 3); // year, month, day
console.log(d.toDateString());
Set a value past its normal range and the date rolls forward (or back) into the next unit automatically:
const d = new Date(2026, 0, 31); // Jan 31
d.setDate(32); // "January 32nd"
console.log(d.toDateString());
This rollover is what makes adding time simple. Read the current value, add to it, set it back:
function addDays(date, n) {
const copy = new Date(date);
copy.setDate(copy.getDate() + n);
return copy;
}
const assigned = new Date(2026, 9, 2);
console.log(addDays(assigned, 30).toDateString()); // crosses into November
console.log(addDays(assigned, -2).toDateString()); // negative goes back
setDate is safer than adding 24 * 60 * 60 * 1000 milliseconds. On the night clocks change for daylight saving, a day is 23 or 25 hours long; setDate handles that, raw milliseconds do not.
Day 0 of a month rolls back to the last day of the previous month. That gives a one-line way to find how many days any month has, leap years included:
function daysInMonth(year, monthIndex) {
return new Date(year, monthIndex + 1, 0).getDate();
}
console.log(daysInMonth(2026, 0)); // 31 (January)
console.log(daysInMonth(2026, 1)); // 28 (February)
console.log(daysInMonth(2028, 1)); // 29 (February, leap year)
Adding a month to January 31st asks for "February 31st," which does not exist, so it rolls over into March:
const d = new Date(2026, 0, 31); // Jan 31
d.setMonth(d.getMonth() + 1);
console.log(d.toDateString()); // "Tue Mar 03 2026" (not Feb 28!)
If you need "same day next month, or the last day if it does not exist," clamp the day using daysInMonth before setting it.
const due = new Date(2026, 9, 2, 23, 59);
const turnedIn = new Date(2026, 9, 3, 8, 5);
if (turnedIn > due) {
console.log("Late");
}
What does this log?
const d = new Date(2026, 11, 30); // Dec 30
d.setDate(d.getDate() + 5);
console.log(d.toDateString());
Mon Jan 04 2027. December 35th rolls over into January of the next year, all handled automatically.What does this log?
const a = new Date(2026, 0, 1);
const b = a;
b.setFullYear(2030);
console.log(a.getFullYear());
2030. b = a does not copy the Date; both names point to the same object, and set methods change that object. Copy with new Date(a) first.What does new Date(2027, 2, 0).getDate() give?
03128addDays to find the date 100 days from todaydaysInMonthsetHours(24, 0, 0, 0) and explain the result in your notes