2009-04-07 3 views
3

У меня есть NSDate, который я получаю от UIDatepicker.UIDatePicker и NSDate

IBOutlet UIDatePicker *dueDate; 
NSDate *selectedDate = [dueDate date]; 

Как получить месяц от dueDate в виде int? (1 за январь, 2 для февраля и т.д.)

ответ

9
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]]; 
NSInteger month = [components month]; 
1
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease]; 
int month = [[dateFormat stringFromDate:dueDate] intValue]; 
Смежные вопросы