2014-01-16 50 views
4

我想要做的是獲取每個月的特定日期在兩個日期之間經過的次數。兩個日期之間某一天過去多少次?

例如: 我希望在2011年11月21日至2013年5月15日之間,每個月的16號已經過了多少次。 在這種情況下,它應該是17.

我一直在最難的時候弄清楚這一點。我知道如何去解決這個問題的唯一方法是編寫一堆if語句來檢查已經過了多少年,然後通過了多少個月等等。有沒有更快的方法,比如NSDate或者其他什麼?

+3

不,我想你可以從起始日期到迭代結束日期,通過'NSDateComponents'對象遞增月/年,將用於創建比較'NSDate'(2011年1月16日,2011年2月16日... 2012年1月16日等)。 – trojanfoe

回答

2

OK,這裏的工作示例的gist。代碼的膽量是:

const NSCalendarUnit units = NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSCalendarCalendarUnit|NSTimeZoneCalendarUnit; 
NSDateComponents *comps = [calendar components:units 
             fromDate:startDate]; 
if ([comps day] > wantDay) 
    nextMonth(comps);  // Missed the first month 
[comps setDay:wantDay]; 

NSInteger count = 0; 
while (YES) { 
    NSDate *compareDate = [calendar dateFromComponents:comps]; 
    if (!compareDate) { 
     NSLog(@"Failed to create compare date"); 
     return 5; 
    } 
    NSLog(@"Comparing %@", [formatter stringFromDate:compareDate]); 
    if ([compareDate compare:endDate] == NSOrderedDescending) 
     break; 
    count++; 
    nextMonth(comps); 
} 

其中nextMonth()是:

static void nextMonth(NSDateComponents *comps) { 
    NSInteger month = [comps month]; 
    if (month == 12) { 
     [comps setYear:[comps year] + 1]; 
     [comps setMonth:1]; 
    } else { 
     [comps setMonth:month + 1]; 
    } 
} 

輸出示例:

$ ./dateiter 19990112 19990212 12 
2014-01-16 13:06:53.189 dateiter[16740:707] start-date=1999-01-12 00:00:00 +0000, end-date=1999-02-12 00:00:00 +0000, want-day=12 
2014-01-16 13:06:53.189 dateiter[16740:707] Comparing 19990112 
2014-01-16 13:06:53.190 dateiter[16740:707] Comparing 19990212 
2014-01-16 13:06:53.190 dateiter[16740:707] Comparing 19990312 
2014-01-16 13:06:53.190 dateiter[16740:707] 2 months 

$ ./dateiter 20111121 20130515 16 
2014-01-16 13:10:53.318 dateiter[16810:707] start-date=2011-11-21 00:00:00 +0000, end-date=2013-05-15 00:00:00 +0000, want-day=16 
2014-01-16 13:10:53.319 dateiter[16810:707] Comparing 20111216 
2014-01-16 13:10:53.319 dateiter[16810:707] Comparing 20120116 
2014-01-16 13:10:53.319 dateiter[16810:707] Comparing 20120216 
2014-01-16 13:10:53.319 dateiter[16810:707] Comparing 20120316 
2014-01-16 13:10:53.320 dateiter[16810:707] Comparing 20120416 
2014-01-16 13:10:53.320 dateiter[16810:707] Comparing 20120516 
2014-01-16 13:10:53.320 dateiter[16810:707] Comparing 20120616 
2014-01-16 13:10:53.320 dateiter[16810:707] Comparing 20120716 
2014-01-16 13:10:53.321 dateiter[16810:707] Comparing 20120816 
2014-01-16 13:10:53.321 dateiter[16810:707] Comparing 20120916 
2014-01-16 13:10:53.321 dateiter[16810:707] Comparing 20121016 
2014-01-16 13:10:53.322 dateiter[16810:707] Comparing 20121116 
2014-01-16 13:10:53.322 dateiter[16810:707] Comparing 20121216 
2014-01-16 13:10:53.322 dateiter[16810:707] Comparing 20130116 
2014-01-16 13:10:53.322 dateiter[16810:707] Comparing 20130216 
2014-01-16 13:10:53.323 dateiter[16810:707] Comparing 20130316 
2014-01-16 13:10:53.323 dateiter[16810:707] Comparing 20130416 
2014-01-16 13:10:53.323 dateiter[16810:707] Comparing 20130516 
2014-01-16 13:10:53.323 dateiter[16810:707] 17 months 
+0

這看起來不錯!還沒有真正挖掘NSDateComponents的能力,所以這將是一個很好的學習經驗。當我有機會的時候,我肯定會試試這個週末。謝謝! –

4

試試這個,

//your dates 
//NSDate *fromDateTime = dateFromString:@"11-21-2011"; 
//NSDate *toDateTime = dateFromString:@" 5-15-2013"; 

NSDate *fromDate; 
NSDate *toDate; 

NSCalendar *calendar = [NSCalendar currentCalendar]; 

[calendar rangeOfUnit:NSMonthCalendarUnit | NSDayCalendarUnit startDate:&fromDate 
      interval:NULL forDate:fromDateTime]; 
[calendar rangeOfUnit:NSMonthCalendarUnit | NSDayCalendarUnit startDate:&toDate 
      interval:NULL forDate:toDateTime]; 

NSDateComponents *difference = [calendar components:NSMonthCalendarUnit | NSDayCalendarUnit 
              fromDate:fromDate toDate:toDate options:0]; 

NSInteger occurance = [difference month]; 

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:fromDate]; 
NSInteger fromDay = [components day]; 

if (fromDay < 16) { 
    occurance = occurance + 1; 
} 

components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:toDate]; 
NSInteger toDay = [components day]; 
if (toDay > 16) { 
    occurance = occurance + 1; 
} 

//now occurance will be number of times the 16th of each month has passed between fromDate and toDate 
相關問題