如何检查数组中连续的工作日
How to check consecutive weekdays in an array
我下面有一个函数 (datesCheck) 循环遍历一组日期,如果一天超过一个,则首先删除所有条目,然后检查日期在数组中是否连续,返回连续的次数从当前日期算起的天数。
func datesCheck(_ dateArray: [Date]) -> Int {
let calendar = Calendar.current
let uniqueDates = NSSet(array: dateArray.map { calendar.startOfDay(for: [=10=]) }).sorted {
([=10=] as AnyObject).timeIntervalSince1970 > ( as AnyObject).timeIntervalSince1970
} as! [Date]
var lastDate = calendar.startOfDay(for: Date())
var i = 0
while i < uniqueDates.count && uniqueDates[i].compare(lastDate) == .orderedSame {
lastDate = (calendar as NSCalendar).date(byAdding: .day, value: -1, to: lastDate, options: [])!
i += 1
}
numberOfConsecutiveDays = i
return i
}
此功能运行良好,但我只想将其应用于周一至周五的日期,连续日期检查器检查周五至周一,有效地忽略周六和周日。我曾尝试使用 calendar.components 来实现此目的,但在检查日期是否连续(不包括周末)时找不到忽略周末的方法。
let today = Date()
let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
let components = calendar!.components([.weekday], fromDate: today)
if components.weekday == 2 {
print("Monday")
} else {
print("Monday")
}
几点:
因为不需要周末,所以将其过滤掉
您的函数是不确定的,因为它使用当前时间 (Date()
)。每个 运行 的结果在理论上是不同的。我添加了第二个参数 fromDate
以使其具有确定性。
func datesCheck(_ dates: [Date], fromDate: Date = Date()) -> Int {
let calendar = Calendar.current
let weekdays = dates.map { calendar.startOfDay(for: [=10=]) }
.filter { 2...6 ~= calendar.component(.weekday, from: [=10=]) }
let uniqueDates = Set(weekdays).sorted(by: >)
var i = 0
var lastDate = calendar.startOfDay(for: fromDate)
while i < uniqueDates.count && uniqueDates[i] == lastDate {
switch calendar.component(.weekday, from: uniqueDates[i]) {
case 2:
// When the lastDate is a Monday, the previous weekday is 3 days ago
lastDate = calendar.date(byAdding: .day, value: -3, to: lastDate)!
default:
// Otherwise, it's 1 day ago
lastDate = calendar.date(byAdding: .day, value: -1, to: lastDate)!
}
i += 1
}
return i
}
测试:
let dates = [
DateComponents(calendar: .current, year: 2017, month: 8, day: 29).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 28).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 25).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 24).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 22).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 21).date!
]
let today = DateComponents(calendar: .current, year: 2017, month: 8, day: 29).date!
print(datesCheck(dates, fromDate: today)) // 4
代码打印 4,因为根据周末规则忽略 28 日和 25 日之间的间隔。
我下面有一个函数 (datesCheck) 循环遍历一组日期,如果一天超过一个,则首先删除所有条目,然后检查日期在数组中是否连续,返回连续的次数从当前日期算起的天数。
func datesCheck(_ dateArray: [Date]) -> Int {
let calendar = Calendar.current
let uniqueDates = NSSet(array: dateArray.map { calendar.startOfDay(for: [=10=]) }).sorted {
([=10=] as AnyObject).timeIntervalSince1970 > ( as AnyObject).timeIntervalSince1970
} as! [Date]
var lastDate = calendar.startOfDay(for: Date())
var i = 0
while i < uniqueDates.count && uniqueDates[i].compare(lastDate) == .orderedSame {
lastDate = (calendar as NSCalendar).date(byAdding: .day, value: -1, to: lastDate, options: [])!
i += 1
}
numberOfConsecutiveDays = i
return i
}
此功能运行良好,但我只想将其应用于周一至周五的日期,连续日期检查器检查周五至周一,有效地忽略周六和周日。我曾尝试使用 calendar.components 来实现此目的,但在检查日期是否连续(不包括周末)时找不到忽略周末的方法。
let today = Date()
let calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
let components = calendar!.components([.weekday], fromDate: today)
if components.weekday == 2 {
print("Monday")
} else {
print("Monday")
}
几点:
因为不需要周末,所以将其过滤掉
您的函数是不确定的,因为它使用当前时间 (
Date()
)。每个 运行 的结果在理论上是不同的。我添加了第二个参数fromDate
以使其具有确定性。
func datesCheck(_ dates: [Date], fromDate: Date = Date()) -> Int {
let calendar = Calendar.current
let weekdays = dates.map { calendar.startOfDay(for: [=10=]) }
.filter { 2...6 ~= calendar.component(.weekday, from: [=10=]) }
let uniqueDates = Set(weekdays).sorted(by: >)
var i = 0
var lastDate = calendar.startOfDay(for: fromDate)
while i < uniqueDates.count && uniqueDates[i] == lastDate {
switch calendar.component(.weekday, from: uniqueDates[i]) {
case 2:
// When the lastDate is a Monday, the previous weekday is 3 days ago
lastDate = calendar.date(byAdding: .day, value: -3, to: lastDate)!
default:
// Otherwise, it's 1 day ago
lastDate = calendar.date(byAdding: .day, value: -1, to: lastDate)!
}
i += 1
}
return i
}
测试:
let dates = [
DateComponents(calendar: .current, year: 2017, month: 8, day: 29).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 28).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 25).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 24).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 22).date!,
DateComponents(calendar: .current, year: 2017, month: 8, day: 21).date!
]
let today = DateComponents(calendar: .current, year: 2017, month: 8, day: 29).date!
print(datesCheck(dates, fromDate: today)) // 4
代码打印 4,因为根据周末规则忽略 28 日和 25 日之间的间隔。