views:

910

answers:

3

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)

+4  A: 

use -[NSCalendar components:fromDate:] for instance the example here:

http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html

Graham Lee
+1  A: 
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];
andi
+7  A: 
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];
dbarker