获得一个月的最后一天

我怎样才能得到当月的最后一天作为一个NSDate

 NSDate *curDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components // set last of month [comps setMonth:[comps month]+1]; [comps setDay:0]; NSDate *tDateMonth = [calendar dateFromComponents:comps]; NSLog(@"%@", tDateMonth); 

也应该工作。

 // 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); 

把它放在一个类别中:

 -(NSDate*)lastDayOfMonth { NSInteger dayCount = [self numberOfDaysInMonthCount]; NSDateComponents *comp = [[self calendar] components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self]; [comp setDay:dayCount]; return [[self calendar] dateFromComponents:comp]; } -(NSInteger)numberOfDaysInMonthCount { NSRange dayRange = [[self calendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self]; return dayRange.length; } -(NSCalendar*)calendar { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:TIMEZONE]]; return calendar; } 

Swift版本

 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)! } } 

来自iOS 7的版本

只是为了摆脱贬低…只是一小部分,以获得一天的最后一秒。

  // current date NSDate *now = [NSDate new]; // get last day of the current month NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; [calendar setTimeZone:[NSTimeZone systemTimeZone]]; NSRange dayRange = [ calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:now]; NSInteger numberOfDaysInCurrentMonth = dayRange.length; NSDateComponents *comp = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:now]; comp.day = numberOfDaysInCurrentMonth; comp.hour = 24; comp.minute = 0; comp.second = 0; NSDate *endOfMonth = [calendar dateFromComponents:comp]; 

Swift 3.0.1版本

 extension Date { func lastDayOfMonth() -> Date { let calendar = NSCalendar.current let dayRange = calendar.range(of: .day, in: .month, for: self) let dayCount = dayRange?.count var comp = calendar.dateComponents([.year, .month, .day], from: self) comp.day = dayCount return calendar.date(from: comp)! } } 

使用Swift 3和iOS 10,我发现最简单的方法是使用CalendardateInterval(of:for:)

 func monthInterval() -> DateInterval? { let calendar = Calendar.current guard let interval = calendar.dateInterval(of: .month, for: Date()) else { return nil } let duration = interval.duration // Subtract one second because dateInterval(of:for:)'s end date returns the beginning of the following month return DateInterval(start: interval.start, duration: duration - 1) } guard let interval = monthInterval() else { return } 

然后你可以使用interval.startinterval.end来获得你需要的date。

解决方法function:

 -(int)GetLastDayOfMonth:(NSDate *)date { int last_day = 27; NSCalendar *cal=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *compsMonth = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date]; [compsMonth setDay:last_day]; int month = [compsMonth month]; while(TRUE){ [compsMonth setDay:last_day+1]; NSDate *dateFuture = [cal dateFromComponents:compsMonth]; NSDateComponents *futureComps = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:dateFuture]; if(month != [futureComps month]){ return last_day; } last_day+=1; } return last_day; }