2013-01-04 18 views
0

我想從所有NSDate到30天后。但它不能正常工作。從現在不需要的行爲獲取所有NSDates

這裏是我使用的代碼:

for (int i = 0; i < days; i++) { 

     NSDate *today = [self getToday]; 

     NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

     NSDateComponents *components = [[NSDateComponents alloc] init]; 
     components.day = i; 
     NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0]; 

     NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:nextMonth]; 

     NSDateComponents *todayDayComponents = [gregorian components:NSDayCalendarUnit fromDate:today]; 

     nextMonthComponents.day = todayDayComponents.day+i; 
     NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents]; 
     DLog(@"nextMonthDay = %@",nextMonthDay); 

     [months addObject:nextMonthDay]; 

    } 

我得到列表:

nextMonthDay = 2013-01-03 22:00:00 +0000 
nextMonthDay = 2013-01-04 22:00:00 +0000 
nextMonthDay = 2013-01-05 22:00:00 +0000 
nextMonthDay = 2013-01-06 22:00:00 +0000 
nextMonthDay = 2013-01-07 22:00:00 +0000 
nextMonthDay = 2013-01-08 22:00:00 +0000 
nextMonthDay = 2013-01-09 22:00:00 +0000 
nextMonthDay = 2013-01-10 22:00:00 +0000 
nextMonthDay = 2013-01-11 22:00:00 +0000 
nextMonthDay = 2013-01-12 22:00:00 +0000 
nextMonthDay = 2013-01-13 22:00:00 +0000 
nextMonthDay = 2013-01-14 22:00:00 +0000 
nextMonthDay = 2013-01-15 22:00:00 +0000 
nextMonthDay = 2013-01-16 22:00:00 +0000 
nextMonthDay = 2013-01-17 22:00:00 +0000 
nextMonthDay = 2013-01-18 22:00:00 +0000 
nextMonthDay = 2013-01-19 22:00:00 +0000 
nextMonthDay = 2013-01-20 22:00:00 +0000 
nextMonthDay = 2013-01-21 22:00:00 +0000 
nextMonthDay = 2013-01-22 22:00:00 +0000 
nextMonthDay = 2013-01-23 22:00:00 +0000 
nextMonthDay = 2013-01-24 22:00:00 +0000 
nextMonthDay = 2013-01-25 22:00:00 +0000 
nextMonthDay = 2013-01-26 22:00:00 +0000 
nextMonthDay = 2013-01-27 22:00:00 +0000 
nextMonthDay = 2013-01-28 22:00:00 +0000 
nextMonthDay = 2013-01-29 22:00:00 +0000 
nextMonthDay = 2013-01-30 22:00:00 +0000 
nextMonthDay = 2013-03-03 22:00:00 +0000 
nextMonthDay = 2013-03-04 22:00:00 +0000 

在列表的末尾是:

nextMonthDay = 2013-01-29 22:00:00 +0000 
nextMonthDay = 2013-01-30 22:00:00 +0000 
nextMonthDay = 2013-03-03 22:00:00 +0000 
nextMonthDay = 2013-03-04 22:00:00 +0000 

因此,這是不正確。應該有

2013-01-29 22:00:00 +0000 
2013-01-30 22:00:00 +0000 
2013-03-01 22:00:00 +0000 
2013-03-02 22:00:00 +0000 

也許有人可以幫助我呢?

謝謝。

+2

之前甚至開始:你爲什麼取 「今天」 每次迭代?它不僅效率低下,如果算法在午夜運行,您可能會冒兩天的風險。另外,爲什麼每次迭代都創建一個新的日曆? –

+0

(而且不應該是從01-30到02-01而不是03-01的日期?你在這裏弄到一些大的東西。) –

+0

我不知道爲什麼,但今天這個評論幫助了我,我補充說在比周期改變前 components.day = i; 至 components.day + = i; 和它的工作 – Streetboy

回答

2

你想看看日期&時間編程指南,特別是:將組件添加到日期

] (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1)

您需要使用您的循環中的setDay:方法來爲每一天從您開始的參考NSDate生成偏移量的NSDate實例。

 NSLog(@"30 consecutive days!");// just hello. 
            // Set up data outside the loop... 
    NSDate *today = [[NSDate alloc] init]; 
    NSCalendar *gregorian = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
     // Surely you want to do something clever with these beyond logging them immediately, right? Let's put them in an array. They'll be in order and we can use them again later. 
    NSMutableArray *thirtyConsecutiveDays = [[NSMutableArray alloc] initWithCapacity:30]; 
    int days = 30; // This is one less than thirty-one. 
        // Ok, let's loop! 
    for (int i = 0; i < days; i++) { 
      // We're going to want a new one of these each time through the loop. 
     NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
      // We need to tell it how far to offset from the reference NSDate. 
     [offsetComponents setDay:i]; // incrementing by i days. 
            // We will have our current NSDateComponents friend tell our NSCalendar friend what NSDate we want next... 
     NSDate *anotherDate = [gregorian dateByAddingComponents: offsetComponents 
                 toDate:today options:0]; 
      // Now add our latest NSDate iteration to our array. 
     [thirtyConsecutiveDays addObject:anotherDate]; 
    } 
     // This is just logging results. 
    for (NSDate *aDate in thirtyConsecutiveDays) { 
     NSLog(@"%@",aDate); 
    } 
0

我有不同的解決方案爲您

int min = 60*60*24; 
for (int i = 0; i < days; i++) { 

     NSDate *today = [self getToday]; 
     NSDate *newDate = [today dateByAddingTimeInterval:(min*i)]; 
} 

希望它可以幫助你

+1

當DST轉換時有點棘手。那第一條線是什麼? –

+0

我編輯了60 * 60 * 24個計數到1天 –

0

如何:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [[NSDateComponents alloc] init]; 
components.day = 1; 
NSDate *date = [self getToday]; 
for (int i = 1; i < 30 ; i++) { 
    date = [calendar dateByAddingComponents:components toDate:date options:0]; 
    [months addObject:date]; 
} 
+0

沒有魔法,只有初始化一次。將一天添加到數組中的前一個元素並添加它... –