Last active
May 18, 2020 17:38
-
-
Save wdchris/6e7d35e32e473954f109ee00509d032f to your computer and use it in GitHub Desktop.
This file contains 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
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