Created
May 16, 2017 11:25
-
-
Save rhysm94/320e066f1c40fed1b4625c827d7f67b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func daysInMonth(year: Int, month: Int) -> Int { | |
switch (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0), month) { | |
case (true, 2): | |
return 29 | |
case (false, 2): | |
return 28 | |
case (_, 1), (_, 3), (_, 5), (_, 7), (_, 8), (_, 10), (_, 12): | |
return 31 | |
default: | |
return 30 | |
} | |
} | |
print(daysInMonth(year: 2017, month: 1)) // 31 | |
print(daysInMonth(year: 2000, month: 2)) // 29 | |
print(daysInMonth(year: 1700, month: 2)) // 28 | |
print(daysInMonth(year: 2017, month: 2)) // 29 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment