Reputation: 16339
I have an NSDate that I get from a UIDatepicker.
IBOutlet UIDatePicker *dueDate;
NSDate *selectedDate = [dueDate date];
How do I retrieve the month from dueDate
in the form of an int
? (1 for Jan, 2 for Feb, etc.)
Upvotes: 3
Views: 2336
Reputation: 4335
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];
Upvotes: 9
Reputation: 14458
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];
Upvotes: 1
Reputation:
use -[NSCalendar components:fromDate:]
for instance the example here:
Upvotes: 5