从NSDate获取NSNumber的月份

The*_*ner 0 objective-c

我如何从NSDate对象获得NSNumber月份?不是当前月份而是对象月份

Pas*_*cal 7

您应该NSCalendar为此目的使用,因为日期的月份取决于所使用的日历.这大部分时间都是格里高利历,但是啊,让我们超级正确.:)

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents comps = [cal components:NSMonthCalendarUnit fromDate:myDate];
NSLog(@"The month is: %d", [comps month]);
Run Code Online (Sandbox Code Playgroud)