SKU // 63

JavaScript Date Set Methods

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.

// The Set Methods

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 / setMillisecondsSmaller 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());
Sat Oct 02 2027 09:15:00 GMT-0400 (Eastern Daylight Time)

// Set Methods Change the Original

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!)
10

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
Watch out
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());
Thu Dec 03 2020

// Rollover: JavaScript Does the Calendar Math

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());
Sun Feb 01 2026

// Adding Days

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
Sun Nov 01 2026 Wed Sep 30 2026
Tip
Adding days with 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.

// The Day-Zero Trick: Days in a Month

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)
31 28 29

// The Month-End Trap

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!)
Tue Mar 03 2026

If you need "same day next month, or the last day if it does not exist," clamp the day using daysInMonth before setting it.

// Comparing Dates

const due = new Date(2026, 9, 2, 23, 59);
const turnedIn = new Date(2026, 9, 3, 8, 5);

if (turnedIn > due) {
  console.log("Late");
}
Late

// Quick Check

What does this log?

const d = new Date(2026, 11, 30);   // Dec 30
d.setDate(d.getDate() + 5);
console.log(d.toDateString());
Show answer
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());
Show answer
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?

  1. 0
  2. 31
  3. 28
  4. An Invalid Date
Show answer
C. Day 0 of March rolls back to the last day of February, and 2027 is not a leap year, so February has 28 days.

// Try It

  1. Click Open Playground. Move the copy one week ahead and confirm the original is unchanged
  2. Use addDays to find the date 100 days from today
  3. Print the number of days in every month of this year with a loop and daysInMonth
  4. Set a date to midnight tonight with setHours(24, 0, 0, 0) and explain the result in your notes
// Video Walkthroughs
Last updated: Oct 10, 2026, 1:41 AM ET