I have an integer array which is a collection of today's and tomorrow's dates, I want to separate the integer array based on the type of day
let dateCollection = [
1633722900,
1633730500,
1633754910,
1633758913,
1633820400,
1633824000,
1633827600,
1633831200,
1633834800,
1633838400,
1633842000
]
expected result
let today: [Int] = [
1633722900,
1633730500,
1633754910,
1633758913
]
let tomorrow: [Int] = [
1633820400,
1633824000,
1633827600,
1633831200,
1633834800,
1633838400,
1633842000
]
what should i do to separate them, i have made an extension to convert the integer to date or vice versa, and display it as a time, i already create the date to time extension too
func getTimetringFromINT() -> String {
let date = Date(timeIntervalSince1970: TimeInterval(self))
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "id")
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.string(from: date)
}
selector i made, after convert the date to Time string
CodePudding user response:
An efficient way to do this would be to calculate the int value of midnight between today and tomorrow and then split the array based on that value
let calendar = Calendar.current
var today = [Int]()
var tomorrow = [Int]()
if let midnight = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: .now))?.timeIntervalSince1970 {
let limit = Int(midnight)
dateCollection.forEach { $0 < limit ? today.append($0) : tomorrow.append($0) }
}
CodePudding user response:
You can use Calendar.current.ordinality
to compare the day of the year for the various dates. Here's an example that generates dates from today and tomorrow and then filters them back into separate arrays:
let today = Date()
let todayInts = Array(0..<10).map { today.timeIntervalSince1970 TimeInterval($0) }
print("Today:", todayInts,"\n")
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)!
let tomorrowInts = Array(0..<10).map { tomorrow.timeIntervalSince1970 TimeInterval($0) }
print("Tomorrow:", tomorrowInts,"\n")
let allInts = todayInts tomorrowInts
let todayDayOfYear = Calendar.current.ordinality(of: .day, in: .year, for: today)!
let filteredTodayInts = allInts.filter { Calendar.current.ordinality(of: .day, in: .year, for: Date(timeIntervalSince1970: $0)) == todayDayOfYear }
print("Filtered today:", filteredTodayInts,"\n")
let tomorrowDayOfYear = todayDayOfYear 1
let filteredTomorrowInts = allInts.filter { Calendar.current.ordinality(of: .day, in: .year, for: Date(timeIntervalSince1970: $0)) == tomorrowDayOfYear }
print("Filtered tomorrow:", filteredTomorrowInts,"\n")
CodePudding user response:
Another approach is to get the start date of the next day (regardless of daylight saving time changes) with the Calendar
API and then partition the array
let startOfTomorrow = Calendar.current.nextDate(after: Date(),
matching: DateComponents(hour: 0),
matchingPolicy: .nextTime)!
.timeIntervalSince1970
let splitIndex = dateCollection.partition{ $0 < Int(startOfTomorrow) }
let tomorrowDates = dateCollection[..<splitIndex]
let todayDates = dateCollection[splitIndex...]
To be able to run this declare dateCollection
as var