Skip to content

Instantly share code, notes, and snippets.

@chrisgfortes
Created April 30, 2019 23:00
Show Gist options
  • Save chrisgfortes/e4136c4128619518c445cd8965bc2226 to your computer and use it in GitHub Desktop.
Save chrisgfortes/e4136c4128619518c445cd8965bc2226 to your computer and use it in GitHub Desktop.
Get number of days in a specified month using JavaScript?
// Month here is 1-indexed (January is 1, February is 2, etc). This is
// because we're using 0 as the day so that it returns the last day
// of the last month, so you have to add 1 to the month number
// so it returns the correct amount of days
function daysInMonth (month, year) {
return new Date(year, month, 0).getDate();
}
// July
daysInMonth(7,2009); // 31
// February
daysInMonth(2,2009); // 28
daysInMonth(2,2008); // 29
https://github.com/datejs/Datejs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment