Created
June 12, 2019 13:46
-
-
Save jaylyerly/0916b8f14f4a33ba2678b3ceb8a32353 to your computer and use it in GitHub Desktop.
Get the number of days in the month containing the given date.
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
import Foundation | |
let jan1 = Date(timeIntervalSince1970: 24*60*60) | |
let days = (0...11).map { | |
Calendar.current.date(byAdding: .month, value: $0, to: jan1)! | |
} | |
days | |
let daysPerMonth = days.map { | |
Calendar.current.range(of: .day, in: .month, for: $0)!.max() | |
} | |
daysPerMonth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment