Get the Last Day of Next Month in JavaScript


Borislav Hadzhiev

Mon Oct 25 20212 min read


Photo by Jared Erondu

Get the Last Day of Next Month #

To get the last day of the next month, use the Date() constructor to create a day object, passing it the current year, current month + 2, and 0 for the day as parameters. The Date object will contain the last day of the next month.

function getLastDayOfNextMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 2, 0); } // ๐Ÿ‘‡๏ธ Tue Nov 30 ... console.log(getLastDayOfNextMonth());

We passed the following 3 parameters to the Date() constructor:

  1. The year
  2. The month
  3. The day

For the year, we used the Date.getFullYear method.

To get the month, we used the Date.getMonth method and added 2 to the result to get a date object for the month after next month.

This is balanced out, because we passed 0 as the day parameter to the Date() constructor. Specifying a day of 0 means - give me the last day of the prior month.

Basically we go 2 months forward by adding 2 to the return value from the getMonth method and then we go 1 day back, to the last day of next month, by specifying 0 in the days slot.

The getMonth method returns a zero-based month index from 0 to 11, meaning January is 0 and December is 11.

The return value from getMonth is not very relevant in this use case, however it's good to be aware of.

Note that this would also work if the next month is January, the year would get rolled over and we would still get the correct date back.

// ๐Ÿ‘‡๏ธ Sun Jan 31 2027 console.log(new Date(2026, 11 + 2, 0));

We passed 2026 as the year to the Date() constructor. For the month, we specified 11 (December) + 2 to get to the month of February, and we rolled back 1 day by setting the day to 0.

This gives us the last day of January 2027, as expected.

Because of how dates work in JavaScript our solutions still works if the next month is the January of next year.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee