我正在开发一个使用日期、月份、年份的应用程序。我想要当前月份的日期,因为当前月份可以有 28、29、30、31 天。我试图获得当年的月份。但我不知道上面的代码。这是我的代码。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *today = [NSDate date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *yearComponents = [currentCalendar components:NSYearCalendarUnit fromDate:today];
int currentYear = [yearComponents year];
for(int months = 0; months < 12; months++)
{
NSString *monthName = [NSString stringWithFormat:@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear];
NSLog(@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear);
[monthArray addObject:monthName];
}