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(); }
function daysInMonth (month, year) { // Use 1 for January, 2 for February, etc. return new Date(year, month, 0).getDate(); } console.log(daysInMonth(2, 1999)); // February in a non-leap year. console.log(daysInMonth(2, 2000)); // February in a leap year.
2.1m questions
2.1m answers
60 comments
57.0k users