4
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy";
NSDate *date = [dateFormatter dateFromString:@"2011"];
for(int month=0;month<=12;month++)
{
[email protected]"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
NSLog(@"month: %@", monthString);
}
我需要显示特定年份中的所有月份。例如,如果我将年份设置为2011,那么我希望整个12个月在该年份中使用上述码。在XCode 5中显示当年的月份列表
但是答案是,我得到的是一月份的印刷次数为12次,但我需要在特定年份获得整整12个月。
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/ – jrturton
使用NSDateComponents。你正在循环使用“月”(12次),但不在循环中使用这些变量。这是正常的,你没有得到12次相同的事情。 – Larme
有没有特定月份缺失的年份?根据我的经验(仅30年),每年都有12个月的时间。 ;-) –