Craig
Craig

Reputation: 16339

UIDatePicker and NSDate

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

Answers (3)

dstnbrkr
dstnbrkr

Reputation: 4335

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];

Upvotes: 9

andi
andi

Reputation: 14458

NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];

Upvotes: 1

Related Questions