Getting the last day of a month

pradox picture pradox · May 5, 2010 · Viewed 27.1k times · Source

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

Answer

Jonas Schnelli picture Jonas Schnelli · Dec 19, 2010
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);

should also work.