Get firstdate, lastdate of month?

后端 未结 8 1007
不思量自难忘°
不思量自难忘° 2021-02-01 21:29

I want to get firstdate、lastdate of month,I try

NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
[components setDay:1];
self.c         


        
8条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-01 22:03

    Swift 5 getting last day of month - base on different sources

    // -------------------------------------------------------
    // lastDayOfMonth
    // -------------------------------------------------------
    static func lastDayOfMonth(year:Int, month:Int) -> Int {
        let calendar:Calendar = Calendar.current
        var dc:DateComponents = DateComponents()
        dc.year = year
        dc.month = month + 1
        dc.day = 0
        let lastDateOfMonth:Date = calendar.date(from:dc)!
        let lastDay = calendar.component(Calendar.Component.day, from: lastDateOfMonth)
        return lastDay
    }
    

提交回复
热议问题