Emmanuel Akinfulubi

JavaScriptDate

setDate mutates the date in JavaScript

Intermediate · 4 min read

Moving a date forward by a day is one of the first things you will need for a calendar, a reminder or a "due tomorrow" label. The usual way is setDate. In this tutorial you will use it to move 31 January 2026 on by one day, and look closely at three things: what the call gives back, what happens to the original date, and why you can't chain another method onto it.

Two surprises are waiting. setDate changes the date you call it on, and it returns a number, not a date. Once you know both, you will avoid a whole family of bugs. You need to know how to make a date and that getDate() gives the day of the month.

Getters and setters

You already know getters: getDate(), getMonth(), getFullYear(). They read one part of a date. Most of them have a matching setter that changes that part: setDate(day), setMonth(month), setFullYear(year).

Mutating

A method mutates an object when it changes the object itself instead of making a new one. All the set methods on a date mutate. Any other variable that points to the same date object sees the change too, because there is only one object.

JavaScript

const a = new Date(2026, 0, 15);
const b = a;
b.setDate(20);
console.log(a.getDate()); // 20

b = a doesn't copy the date. It gives the same object a second name, so changing b changes a.

Here is the exercise. Have a go yourself first if you like, then read on and we will build it together.

20 setDate mutates, and returns a number

fair

Move a date on by one day with setDate. Show what the call returns, what happened to the original object, and what happens if you try to chain.

Stuck? Three hints, smallest first
  1. 1. Every set* method on Date changes the object in place.
  2. 2. What they return is the new timestamp, not the date, so chaining fails.
  3. 3. setDate(0) is the previous month’s last day, which is occasionally useful.

Step 1: move on by one day

Read the current day, add one, and set it:

Step 1

const d = new Date(2026, 0, 31);
d.setDate(d.getDate() + 1);
console.log(d.toDateString()); // 'Sun Feb 01 2026'

You asked for day 32 of January. January only has 31 days, so the extra day carries into February, just as it does with new Date(2026, 0, 32). d now holds 1 February. The date that was 31 January is gone.

Step 2: what setDate returns

Store the return value and look at it:

Step 2

const e = new Date(2026, 0, 31);
const returned = e.setDate(e.getDate() + 1);
console.log(typeof returned); // 'number'
console.log(returned === e.getTime()); // true

setDate returns the date's new timestamp: the number of milliseconds since 1970 (in UTC), the same number getTime() gives. It does not return the date object.

Step 3: the wrong turn, chaining

Many methods on strings and arrays return something you can call another method on, so this looks natural:

Step 3

const other = new Date(2026, 0, 31);
try {
  other.setDate(1).getFullYear();
} catch (err) {
  console.log(err.constructor.name); // 'TypeError'
  console.log(err.message);          // 'other.setDate(...).getFullYear is not a function'
}

other.setDate(1) is a number, and numbers have no getFullYear method, so JavaScript throws a TypeError. try and catch let the code catch the error and print it instead of stopping. The exact wording of the message can vary between browsers; the error type is always TypeError.

Step 4: the other setters work the same way

Step 4

console.log(typeof new Date(2026, 0, 15).setMonth(5)); // 'number'

setMonth, setFullYear, setHours and the rest all mutate and all return the timestamp.

Step 5: setDate(0) steps back a month

Day 0 means "the day before the 1st", so setDate(0) moves to the last day of the previous month:

Step 5

const backwards = new Date(2026, 2, 15); // 15 March 2026
backwards.setDate(0);
console.log(backwards.toDateString()); // 'Sat Feb 28 2026'
console.log(backwards.getMonth(), backwards.getDate()); // 1 28

Month 2 is March, because months count from 0. After setDate(0), the date is 28 February: month 1, day 28.

Why this matters, and the fix

Say a function receives a date and moves it on a day with setDate. The caller's date has changed too, without the caller knowing. Code that shows "today" and "tomorrow" from one date can end up showing tomorrow twice.

The fix is to copy first, change the copy, and return the copy. new Date(date.getTime()) makes a separate date for the same moment. That is the next exercise.

The full solution

The panel moves 31 January on by a day, prints the return value, catches the chaining error and runs setDate(0) from 15 March. Press Run to see each result.

The answer

const d = new Date(2026, 0, 31);            // 31 January 2026
const returned = d.setDate(d.getDate() + 1); // asked for the 32nd

// Chaining reads well and does not work: the setter handed back a number.
let chainError = '';
try {
  const other = new Date(2026, 0, 31);
  other.setDate(1).getFullYear();
} catch (err) {
  chainError = err.constructor.name;
}

// setDate(0) steps back to the last day of the previous month.
const backwards = new Date(2026, 2, 15);     // 15 March 2026
backwards.setDate(0);

console.log('returned:', returned, typeof returned);
console.log('the object itself moved to', d.toDateString());
console.log('chaining threw a', chainError);
console.log('setDate(0) from 15 March ->', backwards.toDateString());

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('setDate returns a timestamp, not the date', typeof returned, 'number');
test('and that number is the mutated date', returned, d.getTime());
test('the original object moved on', d.getDate(), 1);
test('into February', d.getMonth(), 1);
test('so chaining onto the return value throws', chainError, 'TypeError');
test('setMonth behaves the same way', typeof new Date(2026, 0, 15).setMonth(5), 'number');
test('setDate(0) lands on the last day of February', backwards.getDate(), 28);
test('in month 1', backwards.getMonth(), 1);

d.setDate(d.getDate() + 1) is the standard way to move a date, and it has two properties people trip over. It mutates, so every reference to that object now points at a different day. And it returns a number, so d.setDate(1).getFullYear() is a TypeError — numbers have no getFullYear.

The fix for both is the same and it is the next exercise: copy first, mutate the copy, return the copy.

Common mistakes

  • Expecting setDate to return the date. It returns a number, so d.setDate(1).getFullYear() throws.
  • Forgetting that it changes the original. Every variable pointing to that date sees the new day.
  • Writing setDate(1) to add one day. That sets the day of the month to 1. To add one, use setDate(d.getDate() + 1).

Try it yourself

  1. Write addDays(date, n) that copies date first, so the original stays unchanged.
  2. Move 31 December 2026 on by one day with setDate. Which year do you land in?
  3. Use setDate(0) to find the last day of February 2024, starting from any day in March 2024.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.