Skip to content

Instantly share code, notes, and snippets.

@jaylyerly
Created June 12, 2019 13:46
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 jaylyerly/0916b8f14f4a33ba2678b3ceb8a32353 to your computer and use it in GitHub Desktop.
Save jaylyerly/0916b8f14f4a33ba2678b3ceb8a32353 to your computer and use it in GitHub Desktop.
Get the number of days in the month containing the given date.
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