How do I find the number of days in given month and year using swift

后端 未结 8 602
庸人自扰
庸人自扰 2020-12-05 04:06

I want to find the total number days on given month and year. Example: I want to find total number of days on year = 2015, month = 7

相关标签:
8条回答
  • 2020-12-05 04:15

    First create an NSDate for the given year and month:

    let dateComponents = NSDateComponents()
    dateComponents.year = 2015
    dateComponents.month = 7
    
    let calendar = NSCalendar.currentCalendar()
    let date = calendar.dateFromComponents(dateComponents)!
    

    Then use the rangeOfUnit() method, as described in Number of days in the current month using iPhone SDK?:

    // Swift 2:
    let range = calendar.rangeOfUnit(.Day, inUnit: .Month, forDate: date)
    // Swift 1.2:
    let range = calendar.rangeOfUnit(.CalendarUnitDay, inUnit: .CalendarUnitMonth, forDate: date)
    
    let numDays = range.length
    print(numDays) // 31
    

    Update for Swift 3 (Xcode 8):

    let dateComponents = DateComponents(year: 2015, month: 7)
    let calendar = Calendar.current
    let date = calendar.date(from: dateComponents)!
    
    let range = calendar.range(of: .day, in: .month, for: date)!
    let numDays = range.count
    print(numDays) // 31
    
    0 讨论(0)
  • 2020-12-05 04:19

    Swift 2.0

    func getDaysInMonth(month: Int, year: Int) -> Int
    {
        let calendar = NSCalendar.currentCalendar()
    
        let startComps = NSDateComponents()
        startComps.day = 1
        startComps.month = month
        startComps.year = year
    
        let endComps = NSDateComponents()
        endComps.day = 1
        endComps.month = month == 12 ? 1 : month + 1
        endComps.year = month == 12 ? year + 1 : year
    
        let startDate = calendar.dateFromComponents(startComps)!
        let endDate = calendar.dateFromComponents(endComps)!
    
        let diff = calendar.components(NSCalendarUnit.Day, fromDate: startDate, toDate: endDate, options: NSCalendarOptions.MatchFirst)
    
        return diff.day
    }
    
    let days = getDaysInMonth(4, year: 2015) // April 2015 has 30 days
    print(days) // Prints 30
    

    Swift 1.2

    func getDaysInMonth(month: Int, year: Int) -> Int
    {
        let calendar = NSCalendar.currentCalendar()
    
        let startComps = NSDateComponents()
        startComps.day = 1
        startComps.month = month
        startComps.year = year
    
        let endComps = NSDateComponents()
        endComps.day = 1
        endComps.month = month == 12 ? 1 : month + 1
        endComps.year = month == 12 ? year + 1 : year
    
        let startDate = calendar.dateFromComponents(startComps)!
        let endDate = calendar.dateFromComponents(endComps)!
    
        let diff = calendar.components(NSCalendarUnit.CalendarUnitDay, fromDate: startDate, toDate: endDate, options: NSCalendarOptions.allZeros)
    
        return diff.day
    }
    
    let days = getDaysInMonth(4, 2015) // There were 30 days in April 2015
    println(days) // Prints 30
    
    0 讨论(0)
  • 2020-12-05 04:21

    In extension format, using self to be able to return the number of days more dynamically (Swift 3).

    extension Date {
    
    func getDaysInMonth() -> Int{
        let calendar = Calendar.current
    
        let dateComponents = DateComponents(year: calendar.component(.year, from: self), month: calendar.component(.month, from: self))
        let date = calendar.date(from: dateComponents)!
    
        let range = calendar.range(of: .day, in: .month, for: date)!
        let numDays = range.count
    
        return numDays
    }
    
    }
    
    0 讨论(0)
  • 2020-12-05 04:22

    In order to get number of days and all the dates on given month and year, try this.

    func getAllDates(month: Int, year: Int) -> [Date] {
        let dateComponents = DateComponents(year: year, 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 formatter = DateFormatter()
        formatter.dateFormat = "yyyy MM dd"
        formatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
        var arrDates = [Date]()
        for day in 1...numDays {
            let dateString = "\(year) \(month) \(day)"
            if let date = formatter.date(from: dateString) {
                arrDates.append(date)
            }
        }
    
        return arrDates
    }
    

    Usage:

    let arrDatesInGivenMonthYear = getAllDates(month: 1, year: 2018)
    debugPrint(arrDatesInGivenMonthYear)
    //Output: [2018-01-01 00:00:00 +0000, 2018-01-02 00:00:00 +0000, ... , 2018-01-31 00:00:00 +0000]
    
    let numberOfDays = arrDatesInGivenMonthYear.count
    debugPrint(numberOfDays)
    //Output: 31
    
    0 讨论(0)
  • 2020-12-05 04:24

    Swift 5

    Another way to approach this:

    extension Date {
    
        func daysInMonth(_ monthNumber: Int? = nil, _ year: Int? = nil) -> Int {
            var dateComponents = DateComponents()
            dateComponents.year = year ?? Calendar.current.component(.year,  from: self)
            dateComponents.month = monthNumber ?? Calendar.current.component(.month,  from: self)
            if
                let d = Calendar.current.date(from: dateComponents),
                let interval = Calendar.current.dateInterval(of: .month, for: d),
                let days = Calendar.current.dateComponents([.day], from: interval.start, to: interval.end).day
            { return days } else { return -1 }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 04:28

    What about for a given year?

    The following is printing the number of days in the date's month:

    let range = userCalendar.range(of: .day, in: .year, for: Date())!
    print("Days In Year: \(range.count)") // prints 31
    

    ... and not the number of days in the date's year as expected :/

    0 讨论(0)
提交回复
热议问题