Get the Number of Days in a Month using JavaScript

avatar

Borislav Hadzhiev

3 min

banner

Photo from Unsplash

Table of Contents #

  1. Get the Number of Days in a Month in JavaScript
  2. Get the Number of Days in the Current Month

Get the Number of Days in a Month in JavaScript #

To get the number of days in a month:

  1. Call the new Date() constructor, passing it 0 for the days.
  2. The method will return the date corresponding to the last day of the month.
  3. Call the getDate() method on the result to get the number of days in the month.
index.js
function getDaysInMonth(year, month) { return new Date(year, month, 0).getDate(); } const date = new Date(); const currentYear = date.getFullYear(); const currentMonth = date.getMonth() + 1; // ๐Ÿ‘ˆ๏ธ months are 0-based // ๐Ÿ‘‡๏ธ Current Month const daysInCurrentMonth = getDaysInMonth(currentYear, currentMonth); console.log(daysInCurrentMonth); // ๐Ÿ‘‰๏ธ 31 // ๐Ÿ‘‡๏ธ Other Months const daysInJanuary = getDaysInMonth(2025, 1); console.log(daysInJanuary); // ๐Ÿ‘‰๏ธ 31 const daysInSeptember = getDaysInMonth(2025, 9); console.log(daysInSeptember); // ๐Ÿ‘‰๏ธ 30

We passed the following 3 arguments to the new Date() constructor:

  1. The year for which we want to create a Date object
  2. The month index for which we want to create a Date object. Month indexes are zero-based where 0 is January and 11 is December.
  3. The day for which to create a Date object. When 0 is used for the days, we get back the last day of the previous month. We use this approach to balance out the zero-based month index.
If you're passing the result of calling getMonth() to the function, make sure to add 1 to the month index.
index.js
const date = new Date(); // ๐Ÿ‘‡๏ธ add 1 to get correct value const currentMonth = date.getMonth() + 1;

Specifying 0 for the days parameter, allows us to use intuitive month indexes when using the new Date() constructor.

For example, passing a month index of 2, gives us the last day in February, and not March.

index.js
console.log(new Date(2025, 1, 0)); // ๐Ÿ‘‰๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐Ÿ‘‰๏ธ Fri Feb 28 2025
This returns an object that represents the last day of the month.

The last step is to call the Date.getDate method.

index.js
console.log(new Date(2025, 1, 0).getDate()); // ๐Ÿ‘‰๏ธ 31 console.log(new Date(2025, 2, 0).getDate()); // ๐Ÿ‘‰๏ธ 28

The method returns an integer from 1 to 31, which represents the day of the month for a given date according to local time.

Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.

Get the Number of Days in the Current Month in JavaScript #

To get the number of days in the current month:

  1. Use the new Date() constructor to get a date object that corresponds to the last day of the current month.
  2. Call the getDate() method to get an integer representing the last day of the month.
index.js
function getDaysInCurrentMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐Ÿ‘‰๏ธ 31

We passed the following 3 arguments to the new Date() constructor:

  1. The current year.
  2. The current month index. Month indexes are zero-based so 0 is January and 11 is December. This is why we added 1 to the result.
  3. The day for which to create a date object. When 0 is used as the day, we get back the last day of the previous month. We use this approach to balance out the zero-based month index.

It's quite confusing but the getMonth() method returns a zero-based index.

index.js
const date = new Date('January 04, 2025 05:24:07'); console.log(date.getMonth()); // ๐Ÿ‘‰๏ธ 0

To balance this out, we passed 0 to the days parameter of the new Date() constructor to get the last day of the prior month.

For example, passing a month index of 2, gives us the last day of February and not March.

index.js
console.log(new Date(2025, 1, 0)); // ๐Ÿ‘‰๏ธ Fri January 31 2025 console.log(new Date(2025, 2, 0)); // ๐Ÿ‘‰๏ธ Fri Feb 28 2025
In the function, the new Date() constructor returns an object representing the last day of the current month.

The last step is to call the Date.getDate method.

index.js
function getDaysInCurrentMonth() { const date = new Date(); return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); } const result = getDaysInCurrentMonth(); console.log(result); // ๐Ÿ‘‰๏ธ 31

The method returns an integer (from 1 to 31) that represents the day of the month for a given date.

Getting the integer representation of the last day of the current month is the equivalent of getting the number of days in the month.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev