Get all days of any month with objective-c

A seemingly simple question ... how can I return a list of days for any given month?

NSDate *today = [NSDate date]; //Get a date object for today date NSCalendar *c = [NSCalendar currentCalendar]; NSRange days = [c rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:today]; 

Mostly I want to use this, but replace today , say, the month of January so that I can return all these days

+15
objective-c iphone nsdate
Nov 13 '09 at 19:24
source share
2 answers

Answer Carl works on Mac. The following steps are performed on a Mac or iPhone (no dateWithNaturalLanguageString: available there).

 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; // Set your year and month here [components setYear:2015]; [components setMonth:1]; NSDate *date = [calendar dateFromComponents:components]; NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date]; NSLog(@"%d", (int)range.length); 
+60
Nov 13 '09 at 19:39
source share

You can make your date almost any line:

 NSDate *date = [NSDate dateWithNaturalLanguageString:@"January"]; 

Then the rest of your code will work as is to return NSRange number of days in January.

+2
Nov 13 '09 at 19:37
source share



All Articles