First you need to create a variable to store the difference in minutes. Get the first date as reference and create a collection with it. Then filter the rest of the dates checking if the minute component between the the reference date and the current date are equal to the difference. If true increase the difference and return true otherwise just return false. Add the result of the filter to the collection of a single date:
Try like this:
var diff = 5
if let reference = dates.first {
let filtered = [reference] + dates.dropFirst().filter({
if Calendar.current.dateComponents([.minute], from: reference, to: $0).minute! == diff {
diff += 5
return true
}
return false
})
print(filtered) // [2021-01-06 10:52:15 +0000, 2021-01-06 10:57:15 +0000, 2021-01-06 11:02:15 +0000]
}
3
solved How to filter date array in swift [closed]