从今年开始接下来的12个月

时间:2013-07-09 07:18:57

标签: iphone ios

尝试从当月开始接收当年的下一个十二个月。下面是我正在尝试的代码,但它是从下个月开始挑选的,所以我修改了它,但现在它显示了2014年的当月和2013年的其他月份。

 NSDateFormatter  *dateFormatter   = [[NSDateFormatter alloc] init];
NSDate           *today           = [NSDate date];
NSCalendar       *currentCalendar = [NSCalendar currentCalendar];

NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:today];
int currentMonth = [monthComponents month];

NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:today];
int currentYear  = [yearComponents year];
int nextYear     = currentYear + 1;

int months  = 1;
int year;
for(int m = currentMonth-1; months < 12; m++){

int nextMonth = m % 12;

if(nextMonth < currentMonth){
    year = nextYear;
} else {
    year = currentYear;
}

NSLog(@"%@ %i",[[dateFormatter monthSymbols] objectAtIndex: nextMonth],year);

months++;
}

这是以下链接的输出。

August 2013
September 2013
October 2013
November 2013
December 2013
January 2014
February 2014
March 2014
April 2014

2013-07-09 12:54:48.683 MyEventApp [13562:c07] 2014年5月 2013-07-09 12:54:48.683 MyEventApp [13562:c07] 2014年6月 2013-07-09 12:54:48.683 MyEventApp [13562:c07] 2014年7月

从此链接Get Month and year of 1 year advance in iPhone.

获得帮助

这是我修改后的代码。

July 2014
August 2013
September 2013
October 2013
November 2013
December 2013
January 2014
February 2014
March 2014
April 2014
May 2014
June 2014

1 个答案:

答案 0 :(得分:-1)

更改此行

if(nextMonth < currentMonth)

if(nextMonth < currentMonth-1)