3

我有一个从 UIDatepicker 获得的 NSDate。

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

如何dueDate以 的形式检索月份int?(1 月 1 日,2 月 2 日等)

4

3 回答 3

9
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSMonthCalendarUnit fromDate:[dueDate date]];
NSInteger month = [components month];
于 2009-04-07T21:52:57.983 回答
5

使用-[NSCalendar components:fromDate:]例如这里的例子:

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

于 2009-04-07T21:47:35.983 回答
1
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] initWithDateFormat:@"%m" allowNaturalLanguage:NO] autorelease];
int month = [[dateFormat stringFromDate:dueDate] intValue];
于 2009-04-07T21:50:58.613 回答