Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the last day of a month

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

like image 821
pradox Avatar asked May 05 '10 10:05

pradox


People also ask

How do I get the last day of the month in Excel?

=EOMONTH(A2, -1) - returns the last day of the month, one month before the date in cell A2. Instead of a cell reference, you can hardcode a date in your EOMONTH formula.

How do I get the last day of the current month in C#?

DateTime createDate = new DateTime(year, month, 1). AddMonths(1). AddDays(-1);


1 Answers

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.

like image 112
Jonas Schnelli Avatar answered Oct 16 '22 09:10

Jonas Schnelli