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

后端 未结 8 645
庸人自扰
庸人自扰 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:38

    here is the swift 4.0 version

    func getTotalDate(){
        // choose the month and year you want to look
        var dateComponents = DateComponents()
        dateComponents.year = 2018
        dateComponents.month = 10
    
        let calendar = Calendar.current
        let datez = calendar.date(from: dateComponents)
        // change .month into .year to see the days available in the year
        let interval = calendar.dateInterval(of: .month, for: datez!)!
    
        let days = calendar.dateComponents([.day], from: interval.start, to: interval.end).day!
        print(days)
    }
    

提交回复
热议问题