tags:

views:

109

answers:

2

I've been using this function but I'd like to know what's the most efficient and accurate way to get it.

function daysInMonth(iMonth, iYear) {
   return 32 - new Date(iYear, iMonth, 32).getDate();
}
A: 

How about a lookup table, with a little special casing for February in leap years?

Peter
+10  A: 
function daysInMonth(month,year) 
{
   return new Date(year, month, 0).getDate();
}

Day 0 is the last day in the previous month. Because the month constructor is 0 based, this works nicely. A bit of a hack, but that's basically what your doing by subtracting 32.

FlySwat