Skip to content

Instantly share code, notes, and snippets.

@myawesomehub
Created October 7, 2021 14:09
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 myawesomehub/6e2d2fc6781bd55bbb38688c300b27ef to your computer and use it in GitHub Desktop.
Save myawesomehub/6e2d2fc6781bd55bbb38688c300b27ef to your computer and use it in GitHub Desktop.
A Calendar Extension
extension Calendar {
public func getAllDates(
dateInterval: DateInterval,
dateComponent: DateComponents) -> [Date] {
var dates: [Date] = []
dates.append(dateInterval.start)
enumerateDates(startingAfter: dateInterval.start, matching: dateComponent, matchingPolicy: .nextTime) { date, _, stop in
guard let date = date else {
return
}
if date < dateInterval.end { dates.append(date) } else {
stop = true // It is an inout parameter
}
}
return dates
}
public func getAllRealDays(for month: Date) -> [Date] {
guard
let monthInterval = Key.calendar.dateInterval(of: .month, for: month),
let monthFirstWeek = Key.calendar.dateInterval(of: .weekOfMonth, for: monthInterval.start),
let monthLastWeek = Key.calendar.dateInterval(of: .weekOfMonth, for: monthInterval.end)
else { return [] }
return Key.calendar.getAllDates(
dateInterval: DateInterval(start: monthFirstWeek.start, end: monthLastWeek.end),
dateComponent: DateComponents(hour: 0, minute: 0, second: 0)
)
}
public func getAllWeeks(for month: Date) -> [[Date]] {
var weeks: [[Date]] = []
for day in getAllRealDays(for: month) {
if weeks.last?.count ?? 7 < 7 { weeks[weeks.count-1].append(day) } else {
weeks.append([day])
}
}
return weeks
}
public func isSameDate(this: Date, with: Date) -> Bool {
let order = Calendar.current.compare(this, to: with, toGranularity: .day)
switch order {
case .orderedAscending:
return false
case .orderedDescending:
return false
case .orderedSame:
return true
}
}
public func getAllMonths(forDate: Date) -> [Date] {
return Key.calendar.getAllDates(
dateInterval: Key.calendar.dateInterval(of: .year, for: forDate)!,
dateComponent: DateComponents(day: 1, hour: 0, minute: 0, second: 0)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment