Get days in month

Is there a function to get to the number of days in a month from a date?

Thanks

There’s a bunch of ways out there. In the old days we just kept an array with [0]=365 and 1-12 being the number of days in a month and then special casing month 2 (leap year) by dividing the year by 4 and if there was no remainder adding 1 to month [2]’s days.

Essentially, this is what is under the cover in today’s solutions.

This is off w3 resources:

var getDaysInMonth = function(month,year) {
  // Here January is 1 based
  //Day 0 is the last day in the previous month
 return new Date(year, month, 0).getDate();
// Here January is 0 based
// return new Date(year, month+1, 0).getDate();
};
console.log(getDaysInMonth(1, 2012));
console.log(getDaysInMonth(2, 2012));
console.log(getDaysInMonth(9, 2012));
console.log(getDaysInMonth(12, 2012));