如何获取两个日期之间每个星期五的日期?

How to get dates for every Friday between two dates?

我目前使用以下代码 return 两个日期之间每一天的日期数组,包括今天的日期和最后一个日期本身。这很好用。

func dates(for date: String) -> [String] {
    // first get the endDate
    guard var endDate = Formatter.date.date(from: date) else { return [] }
    // for calendrical calculations you should use noon time
    endDate = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: endDate)!
    // lets get todays noon time to start
    var date = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date())!
    var dates: [String] = []
    // while date less than or equal to end date
    while date <= endDate {
        // add the formatted date to the array
        dates.append( Formatter.date.string(from: date))
        // increment the date by one day
        date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
    }

    return dates
}

您只需要在您的方法中添加一个工作日参数,并在将其添加到您的数组之前检查循环中的日期是否为工作日:

extension Formatter {
    static let date = DateFormatter()
}

func dates(for date: String, weekday: Int? = nil) -> [String] {
    Formatter.date.locale = Locale(identifier: "en_US_POSIX")
    Formatter.date.dateFormat = "yyyy-MM-dd"
    // first get the endDate
    guard var endDate = Formatter.date.date(from: date) else { return [] }
    // for calendrical calculations you should use noon time
    endDate = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: endDate)!
    // lets get todays noon time to start
    var date = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: Date())!
    var dates: [String] = []
    // while date less than or equal to end date
    while date <= endDate {
        if weekday == nil {
            dates.append(Formatter.date.string(from: date))
            date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
        } else if let weekday = weekday, Calendar.current.component(.weekday, from: date) == weekday {
            // add the formatted date to the array
            dates.append(Formatter.date.string(from: date))
            date = Calendar.current.date(byAdding: .weekOfYear, value: 1, to: date)!
        } else {
            date = Calendar.current.date(byAdding: .day, value: 1, to: date)!
        }
    }
    return dates
}

dates(for: "2019-12-25")  // ["2019-10-23", "2019-10-24", "2019-10-25", "2019-10-26", "2019-10-27", "2019-10-28", "2019-10-29", "2019-10-30", "2019-10-31", "2019-11-01", "2019-11-02", "2019-11-03", "2019-11-04", "2019-11-05", "2019-11-06", "2019-11-07", "2019-11-08", "2019-11-09", "2019-11-10", "2019-11-11", "2019-11-12", "2019-11-13", "2019-11-14", "2019-11-15", "2019-11-16", "2019-11-17", "2019-11-18", "2019-11-19", "2019-11-20", "2019-11-21", "2019-11-22", "2019-11-23", "2019-11-24", "2019-11-25", "2019-11-26", "2019-11-27", "2019-11-28", "2019-11-29", "2019-11-30", "2019-12-01", "2019-12-02", "2019-12-03", "2019-12-04", "2019-12-05", "2019-12-06", "2019-12-07", "2019-12-08", "2019-12-09", "2019-12-10", "2019-12-11", "2019-12-12", "2019-12-13", "2019-12-14", "2019-12-15", "2019-12-16", "2019-12-17", "2019-12-18", "2019-12-19", "2019-12-20", "2019-12-21", "2019-12-22", "2019-12-23", "2019-12-24", "2019-12-25"]
dates(for: "2019-12-25", weekday: 6) // ["2019-10-25", "2019-11-01", "2019-11-08", "2019-11-15", "2019-11-22", "2019-11-29", "2019-12-06", "2019-12-13", "2019-12-20"]

func firstDayOfTheMonth(until date: String) -> [String] {
    Formatter.date.locale = Locale(identifier: "en_US_POSIX")
    Formatter.date.dateFormat = "yyyy-MM-dd"
    guard let endDate = Formatter.date.date(from: date) else { return [] }
    var date = Date()
    var dates: [String] = []
    // while date less than or equal to end date
    while let firstDayOfTheMonth = Calendar.current.nextDate(after: date, matching: .init(day: 1), matchingPolicy: .nextTime), firstDayOfTheMonth <= endDate {
        dates.append(Formatter.date.string(from: firstDayOfTheMonth))
        date = firstDayOfTheMonth
    }
    return dates
}

firstDayOfTheMonth(until: "2019-12-25")  // ["2019-11-01", "2019-12-01"]

您可以通过以下方式获取一周中任何一天的工作日:

let weekDayIndex = Calendar.current.component(.weekday, from: Date())

星期五恰好是第 5 天。您可以通过以下方式获取任何一天的名称:

print(Calendar.current.weekdaySymbols[weekDayIndex])

因此,只需遍历所有日期并过滤掉工作日不是 5 的所有日期,您就会得到答案:

func fridays(in dates: [Date]) {
  dates.filter { Calendar.current.component(.weekday, from: [=12=]) == 5 }
}