我们怎样才能获得给定月份的星期日数? (迅速)

时间:2016-03-04 23:06:16

标签: swift logic nsdate

我们怎样才能获得某个月的星期日数?平均而言他们是4,但我们可以在指定的月份获得星期日的确切数量, 我不知道从哪里开始,所以任何有关计算星期日或与此相关的任何事情背后的逻辑的提示都将非常感谢并对我有所帮助,

我正在尝试获取给定月份的总天数,并且有点能够使用此代码执行此操作:

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


    let dateComponents = NSDateComponents()
    dateComponents.year = Year
    dateComponents.month = month

    let calendar = NSCalendar.currentCalendar()
    let date = calendar.dateFromComponents(dateComponents)!

    // Swift 2:
    let range = calendar.rangeOfUnit(.Day, inUnit: .Month, forDate: date)

    let numDays = range.length

    return numDays


}

但我想排除一个月的星期日数

谢谢:)

3 个答案:

答案 0 :(得分:2)

您可以使用第一天的星期几计算星期日数并从计数中减去它:

{{1}}

答案 1 :(得分:0)

FileReader("input.txt")

答案 2 :(得分:0)

我在操场上创建了一个类,作为结构返回一周中的任何一天。 我想可以使用enumerateDate。 但无论如何......还不确定卫兵但是来了......

import UIKit

struct CalendarByWeekday {
    var Month = Int()
    var Year = Int()
    var Sundays = [Int]()
    var Mondays = [Int]()
    var Tuesdays = [Int]()
    var Wednesdays = [Int]()
    var Thursdays = [Int]()
    var Fridays = [Int]()
    var Saturdays = [Int]()

}
enum InvalidMonthInput: Error {
    case invalidInput
}


class Calculations {



class func calendar(month:Int ,year: Int) -> CalendarByWeekday {

//MARK: Prevent the input of invalid WeekDay.
    func protectMonth() throws -> Int {
   let monthRange = 1...12
    guard monthRange ~= month
        else {
           print("error")
            throw
            InvalidMonthInput.invalidInput
            //let nilStruct = CalendarByWeekday()
            //return nilStruct
        }
        return month
    }
    do {
        try!protectMonth()

    }

    var results = CalendarByWeekday()
    results.Month = month
    results.Year = year

    //MARK: Set the calendar.

    let calendar = Calendar(identifier: .gregorian)


    //MARK: Get the first day of the month.

    let components = DateComponents.init(calendar: calendar, year: year, month: month, day: 1)
    let firstDayOfTheMonth = calendar.date(from: components)
    let range = calendar.range(of: .day, in: .month, for: firstDayOfTheMonth!)

    //MARK: Get the first weekday of the month.

    let firstWeekdayOfTheMonth = calendar.component(.weekday, from: firstDayOfTheMonth!)

switch firstWeekdayOfTheMonth {
    case 1:
        if range!.count >= 29 {
            results.Sundays = [1,8,15,22,29]
        } else {
            results.Sundays = [1,8,15,22]
        }
        if range!.count >= 30 {
            results.Mondays = [2,9,16,23,30]
        } else {
            results.Mondays = [2,9,16,23]
        }
        if range!.count >= 31 {
            results.Tuesdays   = [3,10,17,24,31]
        } else {
            results.Tuesdays   = [3,10,17,24]
        }
        results.Wednesdays  = [4,11,18,25]
        results.Thursdays  = [5,12,19,26]
        results.Fridays   = [6,13,20,27]
        results.Saturdays  = [7,13,21,28]
    case 2:
        results.Sundays = [7,13,21,28]
        if range!.count >= 29 {
            results.Mondays = [1,8,15,22,29]
        } else {
            results.Mondays = [1,8,15,22]
        }
        if range!.count >= 30 {
            results.Tuesdays   = [2,9,16,23,30]
        } else {
            results.Tuesdays   = [2,9,16,23]
        }
        if range!.count >= 31 {
            results.Wednesdays  = [3,10,17,24,31]
        } else {
            results.Wednesdays  = [3,10,17,24]
        }
        results.Thursdays  = [4,11,18,25]
        results.Fridays   = [5,12,19,26]
        results.Saturdays  = [6,13,20,27]
    case 3:
        results.Sundays = [6,13,20,27]
        results.Mondays = [7,13,21,28]
        if range!.count >= 29 {
            results.Tuesdays   = [1,8,15,22,29]
        } else {
            results.Tuesdays   = [1,8,15,22]
        }
        if range!.count >= 30 {
            results.Wednesdays  = [2,9,16,23,30]
        } else {
            results.Wednesdays  = [2,9,16,23]
        }
        if range!.count >= 31 {
            results.Thursdays  = [3,10,17,24,31]
        } else {
            results.Thursdays  = [3,10,17,24]
        }
        results.Fridays   = [4,11,18,25]
        results.Saturdays  = [5,12,19,26]
    case 4:
        results.Sundays = [5,12,19,26]
        results.Mondays = [6,13,20,27]
        results.Tuesdays   = [7,13,21,28]
        if range!.count >= 29 {
            results.Wednesdays  = [1,8,15,22,29]
        } else {
            results.Wednesdays  = [1,8,15,22]
        }
        if range!.count >= 30 {
            results.Thursdays  = [2,9,16,23,30]
        } else {
            results.Thursdays  = [2,9,16,23]
        }
        if range!.count >= 31 {
            results.Fridays   = [3,10,17,24,31]
        } else {
            results.Fridays   = [3,10,17,24]
        }
        results.Saturdays  = [4,11,18,25]
    case 5:
        results.Sundays = [4,11,18,25]
        results.Mondays = [5,12,19,26]
        results.Tuesdays   = [6,13,20,27]
        results.Wednesdays  = [7,13,21,28]
        if range!.count >= 29 {
            results.Thursdays  = [1,8,15,22,29]
        } else {
            results.Thursdays  = [1,8,15,22]
        }
        if range!.count >= 30 {
            results.Fridays   = [2,9,16,23,30]
        } else {
            results.Fridays   = [2,9,16,23]
        }
        if range!.count >= 31 {
            results.Saturdays  = [3,10,17,24,31]
        } else {
            results.Saturdays  = [3,10,17,24]
        }
    case 6:
        if range!.count >= 31 {
            results.Sundays = [3,10,17,24,31]
        } else {
            results.Sundays = [3,10,17,24]
        }
        results.Mondays = [4,11,18,25]
        results.Tuesdays   = [5,12,19,26]
        results.Wednesdays  = [6,13,20,27]
        results.Thursdays  = [7,13,21,28]
        if range!.count >= 29 {
            results.Fridays   = [1,8,15,22,29]
        } else {
            results.Fridays   = [1,8,15,22]
        }

        if range!.count >= 30 {
            results.Saturdays  = [2,9,16,23,30]
        } else {
            results.Saturdays  = [2,9,16,23]
        }
    case 7:
        if range!.count >= 30 {
            results.Sundays = [2,9,16,23,30]
        } else {
            results.Sundays = [2,9,16,23]
        }
        if range!.count >= 31 {
            results.Mondays = [3,10,17,24,31]
        } else {
            results.Mondays = [3,10,17,24]
        }
        results.Tuesdays   = [4,11,18,25]
        results.Wednesdays  = [5,12,19,26]
        results.Thursdays  = [6,13,20,27]
        results.Fridays   = [7,13,21,28]
        results.Saturdays  = [1,8,15,22,29]
    default:
        print("error")
    }

    return results
}

}

let date = Date()
let myteste = Calculations.calendar(month: 2, year: 2018)

猜你可以创建一个常量,只排除星期日并使用它