Get firstdate, lastdate of month?

后端 未结 8 1031
不思量自难忘°
不思量自难忘° 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:27

    Ok, since I'm rolling my own calendar control, I went ahead and created a NSCalendar category. Here's my lastOfMonth method:

    - (NSDate *)lastOfMonth:(NSDate *)date
    {
        NSDateComponents *dc = [self components:NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit fromDate:date];
    
        NSRange dim = [self rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:date];
        dc.day = dim.length;
    
        return [self dateFromComponents:dc];
    }
    

提交回复
热议问题