///checks the distance between two dates and returns true if there is no distance between them regarding the CalendarFormat
func checkDateDistanceto(dateToCompare: Date, calendarFormat: CalendarFormat) -> Bool {
var distance: Int = 1
switch calendarFormat {
case .day:
guard let startDate = self.getFullHour(), let endDate = dateToCompare.getFullHour() else {
return false
}
let dateComponents = (Calendar.current as NSCalendar).components(.hour, from: startDate, to: endDate, options: [])
if let hour = dateComponents.hour {
distance = hour
} else {
return false
}
case .week, .month:
let dateComponents = (Calendar.current as NSCalendar).components(.day, from: self.startOfDay(), to: dateToCompare.startOfDay(), options: [])
if let day = dateComponents.day {
distance = day
} else {
return false
}
case .year:
guard let startDate = self.startOfMonth(), let endDate = dateToCompare.startOfMonth() else {
return false
}
let dateComponents = (Calendar.current as NSCalendar).components(.day, from: startDate, to: endDate, options: [])
if let month = dateComponents.month {
distance = month
} else {
return false
}
}
if distance > 1 {
return false
} else {
return true
}
}