Skip to content

Instantly share code, notes, and snippets.

@davidglezz
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidglezz/8897869 to your computer and use it in GitHub Desktop.
Save davidglezz/8897869 to your computer and use it in GitHub Desktop.
Gets the number of days in a month (0-11)
// without lookup table
function getMonthDays_A (month, year)
{
if (month === 1)
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
if (month % 2)
return month > 6 ? 31 : 30;
return month > 6 ? 30 : 31;
}
// with lookup table
function getMonthDays_B (month, year)
{
if (month === 1 && year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0))
return 29;
return [31,28,31,30,31,30,31,31,30,31,30,31][month];
}
// mini
function getMonthDays_C (month, year)
{
return (month === 1 && year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) ? 29 : [31,28,31,30,31,30,31,31,30,31,30,31][month];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment