You can create some helpers to get a time insensitive date and the date after a date to compare your dates for equality as shown in this post and group the dates as shown in this post:
extension Date {
var dayAfter: Date { Calendar.current.date(byAdding: .day, value: 1, to: noon)! }
var noon: Date { Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)! }
}
extension Collection where Element == Date {
var grouped: [[Element]] {
return reduce(into: []) {
$0.last?.last?.dayAfter == $1.noon ?
$0[$0.index(before: $0.endIndex)].append($1) :
$0.append([$1])
}
}
}
extension Formatter {
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone.init(secondsFromGMT: 0)!
return formatter
}()
}
let arr = ["2020-09-07 00:00:00 +0000", "2020-09-14 00:00:00 +0000", "2020-09-15 00:00:00 +0000", "2020-09-16 00:00:00 +0000", "2020-09-23 00:00:00 +0000", "2020-10-12 00:00:00 +0000", "2020-10-13 00:00:00 +0000", "2020-10-14 00:00:00 +0000", "2020-10-15 00:00:00 +0000", "2020-10-16 00:00:00 +0000"]
let dates = arr.compactMap(Formatter.iso8601.date)
let grouped = dates.grouped
print(grouped) // [[2020-09-07 00:00:00 +0000], [2020-09-14 00:00:00 +0000, 2020-09-15 00:00:00 +0000, 2020-09-16 00:00:00 +0000], [2020-09-23 00:00:00 +0000], [2020-10-12 00:00:00 +0000, 2020-10-13 00:00:00 +0000, 2020-10-14 00:00:00 +0000, 2020-10-15 00:00:00 +0000, 2020-10-16 00:00:00 +0000]]
// to convert the dates back to strings:
let dateStrings = grouped.map { $0.map(Formatter.iso8601.string) }
print(dateStrings) // [["2020-09-07 00:00:00 +0000"], ["2020-09-14 00:00:00 +0000", "2020-09-15 00:00:00 +0000", "2020-09-16 00:00:00 +0000"], ["2020-09-23 00:00:00 +0000"], ["2020-10-12 00:00:00 +0000", "2020-10-13 00:00:00 +0000", "2020-10-14 00:00:00 +0000", "2020-10-15 00:00:00 +0000", "2020-10-16 00:00:00 +0000"]]