Skip to content

Instantly share code, notes, and snippets.

@Alex1990
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save Alex1990/63e7426fa32826740b05 to your computer and use it in GitHub Desktop.

Select an option

Save Alex1990/63e7426fa32826740b05 to your computer and use it in GitHub Desktop.
Get the number of days in a month in javascript.
/**
* Get the number of days in a month in javascript.
*
* Ref:
* http://stackoverflow.com/questions/315760/what-is-the-best-way-to-determine-the-number-of-days-in-a-month-with-javascript
*/
function daysInMonth(year, month) {
var now = new Date;
month = typeof month != undefined ? month
: (typeof year != undefined ? year : now.getMonth() + 1);
year = typeof month != undefined && typeof year != undefined ? year
: now.getFullYear();
now.setFullYear(year, month, 0);
return now.getDate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment