Skip to content

Instantly share code, notes, and snippets.

@sdthornton
Created December 7, 2012 19:35
Show Gist options
  • Save sdthornton/4235857 to your computer and use it in GitHub Desktop.
Save sdthornton/4235857 to your computer and use it in GitHub Desktop.
Days in a month
monthLength = 31;
if (monthNum==3 || monthNum==5 || monthNum==8 || monthNum==10) {
monthLength = 30;
} else if (monthNum==1) {
if (year % 4 == 0 ) {
if (year % 100 == 0) {
if (year % 400 == 0) {
monthLength = 29;
} else {
monthLength = 28; }
} else {
monthLength = 29; }
} else {
monthLength = 28;}
}
@sdthornton
Copy link
Author

The code first sets the numbers of days in a month to 31. Then it sets April, June, September, and November to 30 days, and then it checks February.

If the year is divisible by 4, e.g. it has a remainder of zero when divided by 4 (year % 4 == 0), and it is divisible by 100, and it is divisible by 400, then there are 29 days. If meets those first two criteria, that is is divisible by 4 and by 100, but not divisible by 400, then there are only 28 days, and if it is divisible by 4, but not by 100, then there are 29 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment