Get the Number of Days in a Month #
To get the number of days in a month:
- Call the
new Date[]
constructor, passing it0
for the days. - The method will return the date corresponding to the last day of the month.
- Call the
getDate[]
method on the result to get the number of days in the month.
Copied!
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]; // 👇️ 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:
- The year for which we want to create a
Date
object - The month index for which we want to create a
Date
object. Month indexes are zero based -0
is January and11
is December. - The day for which to create a
Date
object. When0
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.
Copied!
const date = new Date[]; // 👇️ add 1 to get correct value const currentMonth = date.getMonth[] + 1;
Passing 0
for the days parameter, allows us to use intuitive month indexes when using the Date
constructor.
For
example, passing a month index of 2
, gives us the last day in February, and not March.
Copied!
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. The method returns an integer from 1
to 31
, which represents the day of
the month for a given date.
Getting the integer representation of the last day of the month is the equivalent of getting the number of days in the month.
Further Reading #
- Get the Number of Days in the Current Month in JavaScript
- How to get Current Year, Month and Day in JavaScript
Brett DeWoody
56.9k28 gold badges134 silver badges183 bronze badges
asked Jul 26, 2009 at 11:27
1
// Month in JavaScript is 0-indexed [January is 0, February is 1, etc],
// but by using 0 as the day it will give us the last day of the prior
// month. So passing in 1 as the month number will return the last day
// of January, not February
function daysInMonth [month, year] {
return new Date[year, month, 0].getDate[];
}
// July
daysInMonth[7,2009]; // 31
// February
daysInMonth[2,2009]; // 28
daysInMonth[2,2008]; // 29
answered Jul 26, 2009 at 11:36
16
Date.prototype.monthDays= function[]{
var d= new Date[this.getFullYear[], this.getMonth[]+1, 0];
return d.getDate[];
}
answered Jul 26, 2009 at 22:57
kennebeckennebec
100k31 gold badges104 silver badges126 bronze badges
3
The following takes any valid datetime value and returns the number of days in the associated month... it eliminates the ambiguity of both other answers...
// pass in any date as parameter anyDateInMonth
function daysInMonth[anyDateInMonth] {
return new Date[anyDateInMonth.getFullYear[],
anyDateInMonth.getMonth[]+1,
0].getDate[];}
Biruk Abebe
2,2041 gold badge11 silver badges24 bronze badges
answered Jul 26, 2009 at 17:28
Charles BretanaCharles Bretana
140k22 gold badges145 silver badges212 bronze badges
7
Another possible option would be to use Datejs
Then you can do
Date.getDaysInMonth[2009, 9]
Although adding a library just for this function is overkill, it's always nice to know all the options you have available to you :]
yckart
30.8k9 gold badges117 silver badges127 bronze badges
answered Sep 23, 2009 at 8:30
RYFNRYFN
2,8671 gold badge29 silver badges37 bronze badges
2