I have an array of dates and i want to keep the nearest future day from today.
Is there any function to make it easier than comparing each date in a bucle?
I have this code:
var nearestDate: Date?
for match in allMatches {
let actualDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+00:00")
dateFormatter.dateFormat = "dd'-'MM'-'yyyy"
let matchDate = dateFormatter.date(from: match.date ?? "")
nearestDate = dateFormatter.date(from: "01-01-2200")
if let matchDate = matchDate {
if !matchDate.isInPast {
if matchDate < nearestDate! {
nearestDate = matchDate
}
}
}
}
print(nearestDate)
But its not even returning the correct day