Getting the last day of a month

前端 未结 9 756
别跟我提以往
别跟我提以往 2020-12-08 00:24

How can I get the last day of the current month as an NSDate?

9条回答
  •  温柔的废话
    2020-12-08 00:50

    Swift version

    extension NSDate {
        func lastDayOfMonth() -> NSDate {
            let calendar = NSCalendar.currentCalendar()
            let dayRange = calendar.rangeOfUnit(.Day, inUnit: .Month, forDate: self)
            let dayCount = dayRange.length
            let comp = calendar.components([.Year, .Month, .Day], fromDate: self)
    
            comp.day = dayCount
    
            return calendar.dateFromComponents(comp)!
        }
    }
    

提交回复
热议问题