Skip to content

Instantly share code, notes, and snippets.

@wdchris
Last active May 18, 2020 17:38
Show Gist options
  • Save wdchris/6e7d35e32e473954f109ee00509d032f to your computer and use it in GitHub Desktop.
Save wdchris/6e7d35e32e473954f109ee00509d032f to your computer and use it in GitHub Desktop.
struct CalendarRange {
let month: Int
let year: Int
let daysInMonth: Int
}
static func getCalendarRange(withStartDate startDate: Date, withEndDate endDate: Date) -> [CalendarRange] {
let months = Calendar.current.dateComponents([.month], from: startDate, to: endDate).month ?? 1
var result: [CalendarRange] = []
for d in 0...months {
let dateToAdd = Calendar.current.date(byAdding: .month, value: (d * -1), to: endDate)!
let components = Calendar.current.dateComponents([.month, .year], from: dateToAdd)
var days: Int
if d == 0 {
days = CalendarManager.getDay(for: endDate)
} else {
days = CalendarManager.getDaysInMonth(for: dateToAdd)
}
result.append(CalendarRange(month: components.month!, year: components.year!, daysInMonth: days))
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment