how can we get the number of sundays on an given m

2020-07-29 16:11发布

问题:

how can we get the number of sundays from an given month ? in average they are 4 but can we get the exact number of sundays in a specify month , i don't know where to start for this so any hint about the logic behind counting the sundays or any thing related to this will be much appreciated and helpful for me ,

am trying to get the total number of days in a given month and somewhat am able to do that with this code :

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


}

but i want to exclude number of sundays from a month

thanks :)

回答1:

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)!

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

    let numDays = range.length

    // New code starts here:

    var numberOfSundays = 0

    let dateFormatter = NSDateFormatter()

    for day in 1...numDays {

        dateComponents.day = day

        let date = calendar.dateFromComponents(dateComponents)!
        dateFormatter.dateFormat = "EEEE"
        let dayOfWeek = dateFormatter.stringFromDate(date) // Get day of week

        if dayOfWeek == "Sunday" { // Check if it's a Sunday
            numberOfSundays += 1
        }
    }

    return numDays - numberOfSundays
}


回答2:

You can use the day of week of the first day to compute the number of sundays and subtract it from the count:

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


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

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



    let range = calendar.range(of: .day, in: .month, for: date!)

    let numDays = range?.count

    let dow = calendar.component(.weekday, from: date!)
    let sundays = (numDays! + dow) / 7

    return numDays! - sundays


}


回答3:

i created a class on playground to return any days of the week as a struct. i guess is possible to use enumerateDate. but any way..no sure yet about the guard but here come..

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)

guess you could create a constant and only exclude the sundays and use it