Getting the last day of a month

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

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

9条回答
  •  借酒劲吻你
    2020-12-08 00:45

    NSDate *curDate = [NSDate date];
            NSCalendar *currentCalendar = [NSCalendar currentCalendar];
            NSRange daysRange = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth
             forDate:curDate];
            NSDateComponents* comps = [currentCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:curDate];
            [comps setDay:daysRange.length];
            NSDate *maximumDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
    

提交回复
热议问题