Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to find number of days in month using Objective C?

Instead of calculating the month and leap year to do this calculation. Is there any way to check using some Apple Internal APIs to do so?

I found that the Java have something like this:

calendar.getActualMaximum(calendar.DAY_OF_MONTH) 

Is there any similar thing for Objective C? thz u.

like image 396
Tattat Avatar asked May 16 '10 15:05

Tattat


1 Answers

You can use NSCalendar and its rangeOfUnit:inUnit:forDate method. For example, to get the number of days in the current month:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[NSDate date]];
NSUInteger numberOfDaysInMonth = range.length;
like image 195
mipadi Avatar answered Nov 07 '22 03:11

mipadi