2014-02-11 2 views
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-й месяц в этом году, я использовал tis выше код.Показать список месяца в этом конкретном году в XCode 5

Но ответ, я получаю один месяц, как январь, который напечатан в 12 раз, но мне нужно получить целых 12 месяцев в конкретный год.

+1

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/ – jrturton

+0

Использование NSDateComponents. Вы зацикливаете «месяц» (12 раз), но не используете эти переменные внутри своего цикла. Это нормально, что вы не получаете в 12 раз то же самое. – Larme

+0

Есть ли годы, в которые отсутствует конкретный месяц? По моему опыту (всего 30 лет) каждый год есть все 12 месяцев. ;-) –

ответ

1

Попробуйте следующий код.

 //NSInteger startingMonth = 1; 
    NSInteger startingYear = 2014; 

    // we'll need this in several places 
    NSCalendar *cal = [NSCalendar currentCalendar]; 

    // build the first date (in the starting month and year) 
    NSDateComponents *comps = [[NSDateComponents alloc] init]; 
    //[comps setMonth:startingMonth]; 
    [comps setYear:startingYear]; 
    // [comps setDay:1]; 
    NSDate *date = [cal dateFromComponents:comps]; 

    // this is our output format 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"MMMM YYYY"]; 
    NSDateFormatter *format1 = [[NSDateFormatter alloc] init]; 
    [format1 setDateFormat:@"MM yy"]; 
    // we need NSDateComponents for the difference, i.e. at each step we 
    // want to go one month further 
    NSDateComponents *comps2 = [[NSDateComponents alloc] init]; 
    [comps2 setMonth:1]; 

    for (int i= 0; i < 12; i++) { 
     NSLog(@"month list %@", [format1 stringFromDate:date]); 
     NSString *str=[NSString stringWithFormat:@"%@",[formatter stringFromDate:date]]; 
     [mothlist_ary addObject:str]; 
     NSLog(@"GET MONTH %@",mothlist_ary); 
     // add 1 month to date 
     date = [cal dateByAddingComponents:comps2 toDate:date options:0]; 

    } 
1

Попробуйте это ...

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    dateFormatter.dateFormat = @"yyyy"; 
    NSDate *date = [dateFormatter dateFromString:@"2011"]; 

    NSCalendar *cal = [NSCalendar currentCalendar]; 
    NSDateComponents *components = [cal components:(NSMonthCalendarUnit) fromDate:date]; 


    [email protected]"MMMM"; 

    for(int month=1;month<=12;month++) 
    { 
     [components setMonth:(month)]; 
     NSDate *lastMonth = [cal dateFromComponents:components]; 

     NSString * monthString = [[dateFormatter stringFromDate:lastMonth] capitalizedString]; 
     NSLog(@"month: %@", monthString); 
    } 
Смежные вопросы