ios - 如何提高一个月计算中工作日数的准确性?

标签 ios swift

我正在尝试计算一周中的每一天在一个月中有多少天。我使用下面的这个问题/答案作为我想要实现的目标的基础,并且它在大多数情况下都有效。

how can we get the number of sundays on an given month ? ( swift )

不幸的是,如果是工作日,它还会计算下个月的第一天。

我对代码的计算方式不够熟悉,无法理解是否可以在任何地方添加 -1 或其他内容到该月的总天数。

如果有人可以推荐解决方案,我们将不胜感激。

我已经尝试将“numberOfSundays += 1”更改为“numberOfSundays += 0”,因为我认为这可能会导致问题。

func getNumberOfDaysInMonth (month : Int, Year : Int, nameOfDay: String) -> Int? {

    var dateComponents = DateComponents()
    dateComponents.year = Year
    dateComponents.month = month

    let calendar = NSCalendar.current
    let date = calendar.date(from: dateComponents)

    guard let range = calendar.range(of: .day, in: .month, for: date!) else { return nil }

    let numDays = range.endIndex

    // New code starts here:

    var numberOfSundays = 0

    let dateFormatter = DateFormatter()

    for day in 1...numDays {

        dateComponents.day = day

        guard let date = calendar.date(from: dateComponents) else { return nil }

        dateFormatter.dateFormat = "EEEE"
        let dayOfWeek = dateFormatter.string(from: date) // Get day of week

        if dayOfWeek == nameOfDay { // Check if it's a Monday
            numberOfSundays += 1
        }
    }

    return numberOfSundays
}

getNumberOfDaysInMonth(month: 06, Year: 2019, nameOfDay: "Monday")

getNumberOfDaysInMonth(month: 06, Year: 2019, nameOfDay: "Tuesday")

getNumberOfDaysInMonth(month: 06, Year: 2019, nameOfDay: "Wednesday")

getNumberOfDaysInMonth(month: 06, Year: 2019, nameOfDay: "Thursday")

getNumberOfDaysInMonth(month: 06, Year: 2019, nameOfDay: "Friday")

在我调用函数“getNumberOfDaysInMonth(month: 06, Year: 2019, nameOfDay: "Monday")”的地方,它返回 5 作为 2019 年 6 月的天数,实际上有 4。

最佳答案

func getNumberOfDaysInMonth(month: Int , year: Int, nameOfDay: String) -> Int? {

    var components = DateComponents()
    components.year = year
    components.month = month

    let calendar = Calendar.current

    guard let date = calendar.date(from: components),
        let range = calendar.range(of: .day, in: .month, for: date) else { return nil }

    return range
        .map { DateComponents(year: year, month: month, day: $0) }
        .map { calendar.date(from: $0) }
        .compactMap { date  -> String? in
            guard let date = date else { return nil }
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "EEEE"
            return dateFormatter.string(from: date)
        }
        .filter { $0 == nameOfDay }
        .count
}

关于ios - 如何提高一个月计算中工作日数的准确性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55525413/

相关文章:

ios - 错误 :Use of 'self' in property access 'content' before self. init 初始化自身

ios - 单击 UITextField,我想运行一些代码

ios - 多个 UICollectionView 在同一布局中滚动

ios - 如何快速制作具有可水平调整单元格大小的collectionView

ios - 通过 NSSet 和 NSDictionary 进行迭代的大 O 表示法是什么

ios - 人脸检测和图像放置在它上面

swift - 如何在拖动项目时更改预览图像

ios - 无法通过 Facebook 验证 iOS 应用

objective-c - 实现协议(protocol)的问题

ios - Swift - Grand Central Dispatch 与 block /闭包