Getting the last day of a month

前端 未结 9 785
别跟我提以往
别跟我提以往 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

    // Adapted from running code in my app
    
    NSDate *curDate = [NSDate date];
    NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSRange daysRange = 
    [currentCalendar 
     rangeOfUnit:NSDayCalendarUnit 
     inUnit:NSMonthCalendarUnit 
     forDate:curDate];
    
    // daysRange.length will contain the number of the last day
    // of the month containing curDate
    
    NSLog(@"%i", daysRange.length);
    

提交回复
热议问题