How to Get the First Day of Next Month in JavaScript?

To get the first day of next month,

  • Use the Date() constructor to get the current date time object.
  • Call the getMonth() method on the date object to get the current month of the year
  • Get the next month by adding +1 to the current month
  • If the next month’s index is 12, add +1 to the year and set the next month to 0(Jan).
  • Pass the year, month and day(1) as arguments to the Date() constructor to get the next month’s date object.

For example,

// today's date
const today = new Date();

// Get next month's index(0 based)
const nextMonth = today.getMonth() + 1;

// Get year
const year = today.getFullYear() + (nextMonth === 12 ? 1: 0);

// Get first day of the next month
const firstDayOfNextMonth = new Date(year, nextMonth%12, 1);

console.log(firstDayOfNextMonth);
// Output: 👉 Sat Apr 01 2023 00:00:00 GMT+0530 (India Standard Time)

The getMonth() method returns an integer between 0 and 11 representing the month of the year. Value 0 represents January, 1 represents February, and so on.

If you call the getMonth() method on the current date-time object, it gives you the current month’s index.

For example,

// today's date
const today = new Date();

console.log(today.getMonth());
// Output: 👉 2

The getFullYear() method on the other hand returns an integer value representing the year for the specified date.

If you call the getFullYear() method on the current date-time object, it gives you the current year.

For example,

// today's date
const today = new Date();

console.log(today.getFullYear());
// Output: 👉 2023

Get the First Day of the Current Month

Getting the first day of the current month is quite straightforward.

You can first get the current year and current month using the getFullYear() and getMonth() methods respectively and then pass them as first and second parameters to the Date() constructor.

The third parameter will be a constant value of 1 denoting the first day of the month.

For example,

// Get today's date
const today = new Date();

// Get current year
const year = today.getFullYear();

// Get current month(0 based)
const month = today.getMonth();

// Keep day as 1 to get first day
const day = 1;

// Get first day of current month
const firstDayOfMonth = new Date(year, month, day);

console.log(firstDayOfMonth);
// Output: 👉 Wed Mar 01 2023 00:00:00 GMT+0530 (India Standard Time)

Actually, you can get the first day of any month by keeping the third parameter of the Date() constructor as 1.

The first and second parameters i.e. the year and month can vary based on your requirement.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts