Big Data/Analytics Zone is brought to you in partnership with:
DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

GetLastDateOfMonth(year, Month)

01.20.2006
| 3134 views |
  • submit to reddit
        
function getLastDateOfMonth(year, month){
  return (new Date( (new Date(year,month+1,1))-1 )).getDate();
}

JSAN style test code is here.

ok( getLastDateOfMonth(2000, 0) == 31 );
ok( getLastDateOfMonth(2000, 1) == 29 );
ok( getLastDateOfMonth(2001, 1) == 28 );
ok( getLastDateOfMonth(2004, 1) == 29 );
ok( getLastDateOfMonth(2100, 1) == 28 );