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個月的時間。 ;-) –