如何检查数组中的连续工作日 [英] How to check consecutive weekdays in an array

查看:65
本文介绍了如何检查数组中的连续工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一个函数(datesCheck),它循环遍历日期数组,如果每天有多个条目,则首先删除所有条目,然后检查数组中的日期是否连续,并从中返回连续的天数当前日期.

I have a function below (datesCheck) that cycles through an array of dates and firstly removes any entries if there is more than one a day and then checks whether the dates are consecutive within the array, returning the number of consecutive days from the current date.

func datesCheck(_ dateArray: [Date]) -> Int {
    let calendar = Calendar.current

    let uniqueDates = NSSet(array: dateArray.map { calendar.startOfDay(for: $0) }).sorted {
    ($0 as AnyObject).timeIntervalSince1970 > ($1 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实现此目的,但是在检查日期是否连续(不包括周末)时找不到忽略周末的方法.

This function works well but I want to only apply this to dates that are Monday – Friday, with the consecutive dates checker checking Friday – Monday, effectively ignoring saturday and sunday. I have tried to achieve this using calendar.components but cannot find a way to ignore weekends when checking if the dates are consecutive excluding weekends.

 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")
 }

推荐答案

几点:

  • 由于您不需要周末,请把它们过滤掉

  • Since you don't need weekends, filter them out

您的函数是不确定的,因为它使用当前时间( Date()).理论上,每次运行的结果都是不同的.我添加了第二个参数 fromDate 使其具有确定性.

Your function is non-deterministic, since it uses the current time (Date()). The result is theoretically different for every run. I added a second parameter fromDate to make it deterministic.

func datesCheck(_ dates: [Date], fromDate: Date = Date()) -> Int {
    let calendar = Calendar.current
    let weekdays = dates.map { calendar.startOfDay(for: $0) }
                    .filter { 2...6 ~= calendar.component(.weekday, from: $0) } 
    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

由于周末规则忽略了28号和25号之间的间隔,因此代码显示为4.

The code prints 4 since the gap between the 28th and 25th is ignored according to the weekend rule.

这篇关于如何检查数组中的连续工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆